WebRequest error C #

1

I'm doing a POST type request for a URL, but I always get the following error message: "The underlying connection was closed: Unexpected error in a send.".

This request is made through a dll that creates in C #, which is used by Delphi.

What is more strange that if this method is used by C # itself it works perfectly.

Below the code:

public string GerarRequisicaoPOST(string Json, string URL, string Autenticacao, out int RetornoStatus)
    {           
        try
        {

            string dadosPOST = Json;

            var dados = Encoding.UTF8.GetBytes(dadosPOST);

            var requisicaoWeb = WebRequest.CreateHttp(URL);

            requisicaoWeb.Method = "POST";
            requisicaoWeb.ContentType = "application/json";
            requisicaoWeb.Accept = "application/json";
            requisicaoWeb.ContentLength = dados.Length;
            requisicaoWeb.ReadWriteTimeout = 60000;
            requisicaoWeb.UserAgent = "Mozilla/5.0";                
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            if (Autenticacao != "")
            {
                requisicaoWeb.Headers["Authorization"] = Autenticacao;//Adicionando o AuthToken  no Header da requisição
            }


            //precisamos escrever os dados post para o stream
            using (var stream = requisicaoWeb.GetRequestStream())
            {
                stream.Write(dados, 0, dados.Length);
                stream.Close();

            }


            using (var resposta = (HttpWebResponse)requisicaoWeb.GetResponse())
            {

                var streamDados = resposta.GetResponseStream();
                StreamReader reader = new StreamReader(streamDados);
                object objResponse = reader.ReadToEnd();

                var post = objResponse.ToString();

                var statusCode = resposta.StatusCode;
                RetornoStatus = Convert.ToInt32(statusCode);

                streamDados.Close();
                resposta.Close();

                return post;

            }


        }
        catch (Exception ex)
        {
            RetornoStatus = -1;
            return ex.Message;
        }
    }

The same test done through Postman also works.

    
asked by anonymous 20.12.2018 / 12:57

1 answer

1

I managed to resolve. Simply put

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

before

var requisicaoWeb = WebRequest.CreateHttp(URL);

It looks like this:

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            var requisicaoWeb = WebRequest.CreateHttp(URL);

            requisicaoWeb.Method = "POST";
            requisicaoWeb.ContentType = "application/json";
            requisicaoWeb.Accept = "application/json";
            requisicaoWeb.ContentLength = dados.Length;
            requisicaoWeb.ReadWriteTimeout = 60000;
            requisicaoWeb.UserAgent = "Mozilla/5.0";
            requisicaoWeb.ServicePoint.Expect100Continue = false;
    
20.12.2018 / 18:46