Calling the API with HttpClient - POST

0

It's the first time I've developed something like this, so I do not know what might be wrong.

I'm making an API call with HttpClient , like this:

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 code above is getting the error:

  

The underlying connection was closed: Unexpected error in a submission.

I put the return with string empty so that I can only test the connection to the API per hour.

I tested the URL in SoapUi and got it right.

    
asked by anonymous 18.11.2018 / 15:23

1 answer

0

I researched a few things on the subject, following Minelli's comments on the question, indicating that the problem was in HTTPS.

Basically, the problem is to modify the security protocol to be able to access the API. I modified the code for:

private string autenticar()
{
    try
    {
        using (var client = new HttpClient())
        {
            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);

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            var retornos = client.PostAsync(Url + baseUrl, encodedContent).Result;

            var resultJson = retornos.Content.ReadAsStringAsync().Result;
            var retornoToken = Newtonsoft.Json.JsonConvert.DeserializeObject<Token>(resultJson);

            return retornoToken.token;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Where Url and baseUrl are already defined in the class and consist of

Url = "https:\site.com.br";
baseUrl = "\metodo\funcao";

The focus of the solution is on the line:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

In it I explicitly define which security protocol should be used to access the API and do not use the default protocol of the Framework.

Looking further, I found that the .NET Framework 4.6 default protocol is up SecurityProtocolType.Tls12 and I'm using the .NET Framework 4.5.

Search sources

Stack Overflow Stack Overflow (English) Stack Overflow (English)

Thank you to Minelli, without him I would never have worried about the security protocol.

    
20.11.2018 / 16:43