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.WebE xception: The underlying connection was closed: Could
not establish secure channel for SSL/TLS. ---System.Net.WebE xception:
The request was aborted: The connection was closed unexpectedly.
============
System.ObjectDi sposedException : Cannot access a disposed object named
"System.Net.Tls Stream".
Object name: "System.Net.Tls Stream".
============
System.IO.IOExc eption: Unable to write data to the transport
connection. ---System.IO.IOExc eption: Unable to write data to the
transport connection. ---System.Net.Sock ets.SocketExcep tion: 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.Web Exception" 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(str ing url, string request)
{
UTF8Encoding enc = new UTF8Encoding();
byte [] data = enc.GetBytes(re quest);
HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate(url);
req.Method = "POST";
req.ContentLeng th = 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.ReadWriteTi meout = 99999;
req.ServicePoin t.MaxIdleTime = 99999;
Stream reqStream = req.GetRequestS tream();
reqStream.Write (data, 0, data.Length);
reqStream.Close ();
HttpWebResponse resp = (HttpWebRespons e)req.GetRespon se();
if (req.HaveRespon se)
{
if (resp.StatusCod e == HttpStatusCode. OK || resp.StatusCode ==
HttpStatusCode. Accepted)
{
StreamReader reader = new StreamReader(re sp.GetResponseS tream());
return reader.ReadToEn d();
}
else
{
throw new Exception("Requ est failed: " + resp.StatusDesc ription);
}
}
return null;
}
=============== =============== ==
System.Net.WebE xception: The underlying connection was closed: Could
not establish secure channel for SSL/TLS. ---System.Net.WebE xception:
The request was aborted: The connection was closed unexpectedly.
at System.Net.TlsS tream.EndRead(I AsyncResult asyncResult)
at System.Net.Conn ection.ReadCall back(IAsyncResu lt asyncResult)
--- End of inner exception stack trace ---
at System.Net.Http WebRequest.Chec kFinalStatus()
at System.Net.Http WebRequest.EndG etRequestStream (IAsyncResult
asyncResult)
at System.Net.Http WebRequest.GetR equestStream()
at HttpRequestPost XML_Console.Cla ss1.SendRequest (String url, String
request) in c:\projects\htt prequestpostxml _console\class1 .cs:line 176
at HttpRequestPost XML_Console.Cla ss1.Test1() in
c:\projects\htt prequestpostxml _console\class1 .cs:line 53
WebException.St atus = SecureChannelFa ilure
System.Net.WebE xception: The request was aborted: The connection was
closed unexpectedly.
at System.Net.TlsS tream.EndRead(I AsyncResult asyncResult)
at System.Net.Conn ection.ReadCall back(IAsyncResu lt asyncResult)
WebException.St atus = ConnectionClose d
=============== =============== ==
System.ObjectDi sposedException : Cannot access a disposed object named
"System.Net.Tls Stream".
Object name: "System.Net.Tls Stream".
at System.Net.TlsS tream.InnerWrit e(Boolean async, Byte[] buffer,
Int32 offset, Int32 size, AsyncCallback asyncCallback, Object
asyncState)
at System.Net.TlsS tream.BeginWrit e(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback asyncCallback, Object asyncState)
at System.Net.Conn ection.BeginWri te(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.Conn ectStream.Begin Write(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.Conn ectStream.Write (Byte[] buffer, Int32 offset, Int32
size)
at HttpRequestPost XML_Console.Cla ss1.SendRequest (String URL, String
request) in c:\projects\htt prequestpostxml _console\class1 .cs:line 135
at HttpRequestPost XML_Console.Cla ss1.Test1() in
c:\projects\htt prequestpostxml _console\class1 .cs:line 53
=============== =============== =
System.IO.IOExc eption: Unable to write data to the transport
connection. ---System.IO.IOExc eption: Unable to write data to the
transport connection. ---System.Net.Sock ets.SocketExcep tion: An
established connection was aborted by the software in your host machine
at System.Net.Sock ets.Socket.Begi nSend(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object
state)
at System.Net.Sock ets.NetworkStre am.BeginWrite(B yte[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---
at System.Net.Sock ets.NetworkStre am.BeginWrite(B yte[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.TlsS tream.InnerWrit e(Boolean async, Byte[] buffer,
Int32 offset, Int32 size, AsyncCallback asyncCallback, Object
asyncState)
--- End of inner exception stack trace ---
at System.Net.TlsS tream.InnerWrit e(Boolean async, Byte[] buffer,
Int32 offset, Int32 size, AsyncCallback asyncCallback, Object
asyncState)
at System.Net.TlsS tream.BeginWrit e(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback asyncCallback, Object asyncState)
at System.Net.Conn ection.BeginWri te(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.Conn ectStream.Begin Write(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.Conn ectStream.Write (Byte[] buffer, Int32 offset, Int32
size)
at HttpRequestPost XML_Console.Cla ss1.SendRequest (String URL, String
request) in c:\projects\htt prequestpostxml _console\class1 .cs:line 151
at HttpRequestPost XML_Console.Cla ss1.Test1() in
c:\projects\htt prequestpostxml _console\class1 .cs:line 53
WebException: The request was aborted: The connection was closed
unexpectedly.
at System.Net.Conn ectStream.Begin Write(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.Conn ectStream.Write (Byte[] buffer, Int32 offset, Int32
size)
at HttpRequestPost XML_Console.Cla ss1.SendRequest (String URL, String
request) in c:\projects\htt prequestpostxml _console\class1 .cs:line 157
at HttpRequestPost XML_Console.Cla ss1.Test1() in
c:\projects\htt prequestpostxml _console\class1 .cs:line 53
WebException.St atus = ConnectionClose d
=============== =============== ==
Thanks,
Josh Blair
Evergreen, CO