Error Webrequest c#

Asked

Viewed 134 times

1

I am making a POST type request for a URL, but I always receive as an error the following message "The underlying connection was closed: Unexpected error in an upload.".

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

What is more strange than if this method is used by the c# itself 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.

  • Where exactly are you getting the error message? Have you thought about swapping Webrequest for an Httpclient?

  • On return of the request made by Delphi.

  • And where is that code?

1 answer

1

I managed to solve. Just put

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

before the

var requisicaoWeb = WebRequest.CreateHttp(URL);

being 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.