4

I have the following code :

String url = // a valid url
String requestXml = File.ReadAllText(filePath);//opens file , reads all text and closes it
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("DEFAULT\\Admin", "Admin"); 
request.ContentType = "application/xml";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.KeepAlive = false;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
return new StreamReader(responseStream).ReadToEnd();

During run-time, I'm getting the following exception while trying to read the HTTPWebResponse :

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
user1168608
  • 598
  • 2
  • 10
  • 27
  • https://stackoverflow.com/questions/21728773/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-receiv – w0051977 Sep 08 '17 at 10:16
  • @w0051977 I have seen this thread already, i'm already setting request.keepAlive = false. So is it necessary to override the GetWebRequest(Uri uri) method? – user1168608 Sep 08 '17 at 10:26
  • 3
    Remove the Close() method and try again. You are trying to read a response from a connection that you closed. – jdweng Sep 08 '17 at 11:01

3 Answers3

2

Don't close the stream before reading from it. This should work:

String url = // a valid url
String requestXml = File.ReadAllText(filePath);//opens file , reads all text and closes it
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("DEFAULT\\Admin", "Admin"); 
request.ContentType = "application/xml";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.KeepAlive = false;
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            using (StreamReader streamReader = new StreamReader(responseStream))
            {
                return streamReader.ReadToEnd();
            }
        }
    }
}
Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55
2

Adding this line of code solved the problem for me:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

This was taken from: https://our.umbraco.com/forum/using-umbraco-and-getting-started/74628-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send

elvenstone
  • 43
  • 6
0
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(url);
    httpResponse = client.GetAsync(url).Result;
}

Try this

elena.kim
  • 930
  • 4
  • 12
  • 22
Agent 47
  • 33
  • 6