HttpWebRequest and strange networking errors
C#
1
Posts
1
Posters
0
Views
1
Watching
-
I've got the following code that did seem to be working successfully: (It's sending a SOAP envelope to a Java web service and receiving a response)
EndPoint
is just a string property containing the URL and path to the WebService,Response
is aHttpWebResponse
property, that other classes use to process the response SOAP.//create the request HttpWebRequest req = (HttpWebRequest) WebRequest.CreateDefault(new Uri(EndPoint)); //and request header info req.ContentType = "text/xml; charset=utf-8"; req.Method = "POST"; req.Accept = "text/xml"; req.KeepAlive = false; req.Timeout = Timeout; req.Proxy = WebRequest.GetSystemWebProxy(); req.Headers.Add("SOAPAction", EndPoint); //write the SOAP Message to the request using (StreamWriter stm = new StreamWriter(req.GetRequestStream(), Encoding.UTF8)) { stm.Write(soapRequest.ToString(SaveOptions.DisableFormatting)); //remove the formatting from the passed XML. stm.Flush(); } Response = null; try { //get the response Response = ((HttpWebResponse) req.GetResponse()); //read the raw response into ResponseString using (StreamReader reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8)) ResponseString = reader.ReadToEnd(); } catch (WebException we) { throw new ApplicationException("A web exception occurred in calling Transact OUG!\\r\\n" + we.Message, we); } catch (Exception e) { throw new ApplicationException("A general exception occurred when calling Transact OUG!\\r\\n" + e.Message, e); } finally { if (Response != null) Response.Close(); req = null; }
Now I'm not sure what's really changed but I'm getting really odd errors like 'An operation was attempted on something that is not a socket', now I think this might be threading related. As I'm seeing the very first time it returns a timeout, its only the second attempts that I get the odder errors. As I said this code was working fine when it was called via a single Window WPF app, I