By Amzath


2011-03-21 19:26:15 8 Comments

My code making https request. This is my code

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UploadUrl);
            request.Method = "POST";
            request.KeepAlive = false;
            request.Credentials = new NetworkCredential(userid, testpwd);

            postData = "<root></root>";
            request.ContentType = "application/x-www-form-urlencoded";

            byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = postDataBytes.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(postDataBytes, 0, postDataBytes.Length);
            requestStream.Close();

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                var result = responseReader.ReadToEnd();
                responseReader.Close();
                Console.WriteLine(result);
            }

This code was running good but all of a sudden throwing following exception

System.Net.WebException

The exception message is: The underlying connection was closed: An unexpected error occurred on a send.

Stack Trace: at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at CustomerProcessor.Delivery.Deliver(String content, Int32 productCategory, String identifier, String xsltFile)

The Exception has an inner exception: An Exception occurred: System.IO.IOException The exception message is: Received an unexpected EOF or 0 bytes from the transport stream.

Stack Trace: at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Net.TlsStream.CallProcessAuthentication(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.ConnectStream.WriteHeaders(Boolean async)

Is there any chance to see what could be the cause of this issue?

5 comments

@Cosmin 2016-03-17 14:41:18

This worked for me:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I have put that line before creating the web request.

@deebs 2018-04-24 14:12:35

I had to use the solution here to get Tls12 working: community.developer.authorize.net/t5/Integration-and-Testing‌​/…

@Lei Shi 2015-03-12 20:46:29

My experienced is just as elRobbo described. I have to upgrade the Win2k3 server to Win2k8.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 

is only supported in .net 4.5, which is not supported in Win2k3...

@Jon Hanna 2011-03-21 19:52:48

I have no idea why (especially since it goes against docs I've seen on the matter) but I've found that leaving out the assignment of RequestLength. Rather, just write to the request stream without setting the header.

This also has the advantage that:

using(Stream stm = req.GetRequestStream())
using(StreamWriter sw = new StreamWriter(stm))
{
  sw.Write(postData);
}

is to my mind simpler, along with having greater advantages in cases where the data is larger and written piece by piece, or comes from something like an XMLWriter that can be set up to write straight to the stream in question.

@elRobbo 2014-10-22 07:01:53

Another potential cause of this - if you are running on Windows Server 2003 it supports only SSL 2.0, SSL 3.0, TLS 1.0. All of these protocols are now recommended to be disabled in light of recent exploits that have been found for these protocols, so you may find that a tightly secured server just can't be connected to from windows server 2003 whereas it will be fine from your development machine. There's nothing you can really do about this situation other than ask the destination server administrator's team to allow TLS1.0, or upgrade your production server.

Source of SSL support on Win2k3Server: http://blogs.msdn.com/b/kaushal/archive/2011/10/02/support-for-ssl-tls-protocols-on-windows.aspx

@Marvin Zumbado 2015-08-08 06:34:10

You sir are a genius and I owe you a beer. Thanks!

@Mikko Viitala 2016-12-12 11:16:21

You ever bought that beer?

@Petr Dokoupil 2012-09-03 09:05:41

Adding this call before request helped me:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

So if you are using https try to change the default SecurityProtocol.

@Grubsnik 2012-10-24 13:47:50

This just saved my day.. apparently there are still websites out there running on SSL2.0.

Related Questions

Sponsored Content

11 Answered Questions

26 Answered Questions

[SOLVED] Catch multiple exceptions at once?

2 Answered Questions

[SOLVED] How to enable TLS 1.2 for outbound request from windows server 2012 R2?

1 Answered Questions

WCF Rest making a http request: sequence closed due to authentication error

  • 2017-12-01 06:32:20
  • samuel Sai
  • 46 View
  • 0 Score
  • 1 Answer
  • Tags:   c# .net rest api wcf

2 Answered Questions

0 Answered Questions

iOS push notification issue using push sharp library

0 Answered Questions

PowerShell + HttpWebRequest throws exception

2 Answered Questions

[SOLVED] Accessing Webservice which is hosted on https server

1 Answered Questions

[SOLVED] Amazon S3 GetObjectMetadata failed (The underlying connection was closed)?

  • 2010-08-06 14:06:07
  • Shoaib Shaikh
  • 1644 View
  • 1 Score
  • 1 Answer
  • Tags:   .net amazon-s3

Sponsored Content