0
It’s the first time I’ve developed something like this, so I don’t really know what could be wrong.
I’m making a call to an API with the HttpClient
, thus:
private string autenticar()
{
try
{
using (var client = new HttpClient())
{
string baseUrl = "/geosite-telecom-api/auth/token";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Mozilla", "5.0"));
var parameters = new Dictionary<string, string> { { "username", Usuario }, { "password", Senha } };
var encodedContent = new FormUrlEncodedContent(parameters);
var retornos = client.PostAsync(Url + baseUrl + $"?username={Usuario}&password={Senha}", encodedContent).Result;
return "";
}
}
catch (Exception ex)
{
throw ex;
}
}
This above code is getting the error:
The underlying connection was closed: Unexpected error in an upload.
I put the return with string
empty so I can test only the connection to the API per hour.
I tested the URL on Soapui and managed to do it right.
I suspect something related to the HTTPS connection. Hard to be sure without knowing the details of the API being called... Can you share the documentation? A suggestion that can help: use Fiddler to capture the requisitions, both the one successfully made in Soapui and this one via code. So you can compare one call against the other and go correcting.
– Minelli
@Minelli do not know if I can still release the documentation because it seems that they release only upon payment of the service, but I will confirm and if you can put here. Anyway, I’ll test Fiddler and see how it looks. I’ll get back to you. Thanks.
– NDelavi
@Minelli thank you so much man, really the problem was in HTTPS, I just modified the
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
and it worked! Thank you. If you want to put as answer, I mark as answered. Thanks!– NDelavi
Another detail is... you are doing a Post, but passing the parameters by Query String... wouldn’t it be the case to switch to GET? or remove these parameters, leaving only the
encondedContent
?– Leandro Angelo
@Leandroangelo already changed that too, along with Servicepointmanager. Thank you.
– NDelavi