问题描述:

I'm getting this error on just one server running Windows Server 2003:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.


Here's my code... Any ideas?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https:// URL HERE ");

//request.Headers.Add("Accept", "application/xml");

byte[] bytes;

bytes = System.Text.Encoding.ASCII.GetBytes(xml);

request.KeepAlive = false;

request.Accept = "application/xml";

request.ContentType = "application/xml; charset='UTF-8'";

request.ContentLength = bytes.Length;

request.Method = "POST";

request.Timeout = 10000;

request.ServicePoint.Expect100Continue = false;

网友答案:

This problem occurs when the client computer cannot send an HTTP request. The client computer cannot send the HTTP request because the connection has been closed or is unavailable. This problem may occur when the client computer is sending lots of data. To resolve this problem, see resolutions A, D, E, F, and O.

https://support.microsoft.com/en-us/kb/915599

网友答案:

I was getting the same error, using RestSharp with .NET 4.5. I tested the same URL with cURL and it worked fine. After a long time debugging I found that setting the SecurityProtocol fixed the issue.

See: THE UNDERLYING CONNECTION WAS CLOSED: AN UNEXPECTED ERROR OCCURRED ON A SEND : With SSL Certificate

网友答案:

The below code works for me. Can you try this:

    var request = (HttpWebRequest)WebRequest.Create("http:// url");

    var bytes = Encoding.ASCII.GetBytes(xml);

    request.Method = "POST";

    // Set the content type of the data being posted.
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = bytes.Length;

    using (var stream = request.GetRequestStream())
    {
        stream.Write(bytes, 0, bytes.Length);
    }

    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
网友答案:

In my case, I forgot to remove the "s" from "https" when I was swapping URLs between environments. I was hitting Localhost with https on accident. Same thing would occur if you are hitting an http site with no https certificate, or an expired certificate.

相关阅读:
Top