HttpWebRequest intermittent trouble: ConnectionClosed, SecureChannelFailure, and others issues

J

joshblair

Hello,

I am trying to post XML documents to a third party using the
HttpWebRequest. This URL uses HTTPS (SSL) but I don't have a client
certificate to deal with. Apparently they are using WebMethods as the
platform that receives these postings. I don't have any experience
with that technology.

The sample below is the from test app that I put together to post the
XML (cXML Order Requests) documents. These documents, at least my
simple samples aren't very large in size; maybe 4-8 KB in at the most.
The sample SendRequest method works 60-80% of the time in my testing.
The other 20-40% of the time I get a number of Exceptions, mostly
WebExceptions. See below:

============
System.Net.WebException: The underlying connection was closed: Could
not establish secure channel for SSL/TLS. ---> System.Net.WebException:
The request was aborted: The connection was closed unexpectedly.
============
System.ObjectDisposedException: Cannot access a disposed object named
"System.Net.TlsStream".
Object name: "System.Net.TlsStream".
============
System.IO.IOException: Unable to write data to the transport
connection. ---> System.IO.IOException: Unable to write data to the
transport connection. ---> System.Net.Sockets.SocketException: An
established connection was aborted by the software in your host machine
|
L--> (only got this one once so far...)
============
WebException: The request was aborted: The connection was closed
unexpectedly. (inner exception)
============
More exception details below the code sample.

Also, I found a KB article that looked like it might have been the
explanation but I installed a hotfix that updated the System.dll to the
version: 1.1.4322.2038 and I am still getting the same behavior.
=========
FIX: You may receive a "System.Net.WebException" error message when you
upload a large file over a SSL connection in the .NET Framework 1.1
Article ID: 884537
http://support.microsoft.com/kb/884537
=========

I tried setting a few timeout properties and the KeepAlive property of
the HttpWebRequest object with no real noticeable improvement. Any
ideas would be greatly appreciated.

public static string SendRequest(string url, string request)
{
UTF8Encoding enc = new UTF8Encoding();
byte [] data = enc.GetBytes(request);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentLength = data.Length;
req.ContentType = "text/xml";
req.KeepAlive = true;
req.UserAgent = null;

// tried setting these different timeouts with no real improvement
req.Timeout = 99999;
req.ReadWriteTimeout = 99999;
req.ServicePoint.MaxIdleTime = 99999;

Stream reqStream = req.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (req.HaveResponse)
{
if (resp.StatusCode == HttpStatusCode.OK || resp.StatusCode ==
HttpStatusCode.Accepted)
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
return reader.ReadToEnd();
}
else
{
throw new Exception("Request failed: " + resp.StatusDescription);
}
}
return null;
}


================================
System.Net.WebException: The underlying connection was closed: Could
not establish secure channel for SSL/TLS. ---> System.Net.WebException:
The request was aborted: The connection was closed unexpectedly.
at System.Net.TlsStream.EndRead(IAsyncResult asyncResult)
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult
asyncResult)
at System.Net.HttpWebRequest.GetRequestStream()
at HttpRequestPostXML_Console.Class1.SendRequest(String url, String
request) in c:\projects\httprequestpostxml_console\class1.cs:line 176
at HttpRequestPostXML_Console.Class1.Test1() in
c:\projects\httprequestpostxml_console\class1.cs:line 53

WebException.Status = SecureChannelFailure

System.Net.WebException: The request was aborted: The connection was
closed unexpectedly.
at System.Net.TlsStream.EndRead(IAsyncResult asyncResult)
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)

WebException.Status = ConnectionClosed

================================

System.ObjectDisposedException: Cannot access a disposed object named
"System.Net.TlsStream".
Object name: "System.Net.TlsStream".
at System.Net.TlsStream.InnerWrite(Boolean async, Byte[] buffer,
Int32 offset, Int32 size, AsyncCallback asyncCallback, Object
asyncState)
at System.Net.TlsStream.BeginWrite(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback asyncCallback, Object asyncState)
at System.Net.Connection.BeginWrite(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.BeginWrite(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32
size)
at HttpRequestPostXML_Console.Class1.SendRequest(String URL, String
request) in c:\projects\httprequestpostxml_console\class1.cs:line 135
at HttpRequestPostXML_Console.Class1.Test1() in
c:\projects\httprequestpostxml_console\class1.cs:line 53

===============================

System.IO.IOException: Unable to write data to the transport
connection. ---> System.IO.IOException: Unable to write data to the
transport connection. ---> System.Net.Sockets.SocketException: An
established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object
state)
at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.TlsStream.InnerWrite(Boolean async, Byte[] buffer,
Int32 offset, Int32 size, AsyncCallback asyncCallback, Object
asyncState)
--- End of inner exception stack trace ---
at System.Net.TlsStream.InnerWrite(Boolean async, Byte[] buffer,
Int32 offset, Int32 size, AsyncCallback asyncCallback, Object
asyncState)
at System.Net.TlsStream.BeginWrite(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback asyncCallback, Object asyncState)
at System.Net.Connection.BeginWrite(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.BeginWrite(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32
size)
at HttpRequestPostXML_Console.Class1.SendRequest(String URL, String
request) in c:\projects\httprequestpostxml_console\class1.cs:line 151
at HttpRequestPostXML_Console.Class1.Test1() in
c:\projects\httprequestpostxml_console\class1.cs:line 53

WebException: The request was aborted: The connection was closed
unexpectedly.
at System.Net.ConnectStream.BeginWrite(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32
size)
at HttpRequestPostXML_Console.Class1.SendRequest(String URL, String
request) in c:\projects\httprequestpostxml_console\class1.cs:line 157
at HttpRequestPostXML_Console.Class1.Test1() in
c:\projects\httprequestpostxml_console\class1.cs:line 53

WebException.Status = ConnectionClosed

================================

Thanks,

Josh Blair
Evergreen, CO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top