Link to home
Start Free TrialLog in
Avatar of mathew_s
mathew_s

asked on

Cannot Send HTTP Request in Windows Service

Below highlighted is where code gets error. I include some other code as it may be useful. Not sure what is happening as the code worked before for many months and now all of a sudden it stopped working. It seems that it is not able to send the HTTP request. The windows service is running on a Windows 2003 server.

I tried running on a different proxy and without one on the server and still get the same error.

 
HttpWebRequest webRequestExAnte   = CreateWebRequest(_url, _action);

….
                using (WebResponse webResponse = webRequestExAnte.EndGetResponse(asyncResultExAnte))*****errors out saying underlying connection is closed: An unexpected error occurred on a send
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResultExAnte = rd.ReadToEnd();
                    }
                }


        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            webRequest.ProtocolVersion = HttpVersion.Version11;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            ServicePointManager.DefaultConnectionLimit = 200;


            //create the proxy
            ICredentials credentials = new NetworkCredential("X Datafeed", "");
            IWebProxy webProxy = new WebProxy(ConfigurationManager.AppSettings.Get("ProxyAddress") + ":" + ConfigurationManager.AppSettings.Get("ProxyPort"), true);

            webProxy.Credentials = credentials;
            webRequest.Proxy = webProxy;

            //add the certificate
            try
            {
                X509Certificate2 col = new X509Certificate2(ConfigurationManager.AppSettings.Get("Path"), ConfigurationManager.AppSettings.Get("XFeedPwd"), X509KeyStorageFlags.MachineKeySet);

                webRequest.ClientCertificates.Add(col);

            }

            catch (Exception ex)

            {

                EventLog eventlog = new EventLog();
                eventlog.Source = "XFeed";
                eventlog.Log = "XLog";
                eventlog.WriteEntry("Exception encountered in CreateWebRequest: " + ex.Message.ToString(), EventLogEntryType.Warning);

            }

          
            //webRequest.KeepAlive = true;  -- tried this here didn't work
            return webRequest;

        }

Open in new window


 
It looks to be getting an error where the *** are above when I step through the code. Error message is: The underlying connection was closed: An unexpected error occurred on a send.

Any yes I looked at this article http://support.microsoft.com/kb/915599/en-us but still could not resolve.
Avatar of LordWabbit
LordWabbit

Well usually when working code stops working it's due to a change in the environment the code is running in.  Can you do the call from your own PC?  Is there an inner exception with more information?  Have the network admins changed permissions on the proxy/firewall etc.  Have the X Datafeed credentials expired?
check if you could browse the url. Use Fiddler and capture the traffic and check. Most probably it shud be issue with the other end.
Avatar of mathew_s

ASKER

I get the same error on my own PC "The underlying connection was closed: An unexpected error occurred on a send."

Checked the inner exception. This is interesting, it says "Received an unexpected EOF or 0 bytes from the transport stream."

The credentials should be fine but I am getting that checked as well just in case. I had someone check the proxy/firewall but they were not able to find anything.
Capture.JPG
I googled a bit and found this on another site which shall not be named
Apple discontinued support for SSL3.0, which is most likely your issue. Change to TLS and restart your service.

and further on
Look through your service codebase. A find for ssl will probably turn something up. For example, in .NET the SslStream.AuthenticateAsClient() method can be switched from ssl to tls by passing a different member of the System.Security.Authentication.SslProtocols enumeration (Tls rather than Ssl3) as a parameter

The switch from SSL3 to TLS is due to the Heartbleed and POODLE vulnerabilities.  Although the move to TLS is not without it's own issues.
So changing
   ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

Open in new window

to
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

Open in new window

should sort out the error.
Yeah, I tried this before. So I will provide information on the error when changing to TLS.

Below I just changed the securityprotocol.

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            webRequest.ProtocolVersion = HttpVersion.Version11;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            ServicePointManager.DefaultConnectionLimit = 200;

Open in new window


It does not error in that line any more but I don't get anything in the response stream.

                using (WebResponse webResponse = webRequestExAnte.EndGetResponse(asyncResultExAnte))
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResultExAnte = rd.ReadToEnd(); ***nothing here.
                    }

                }

Open in new window


The exception says "Root element is missing". I include some captures that may be useful.
Capture2.jpg
Capture3.jpg
Mmmm, not supported exception?  Might try some of the other tls protocols and see if you get the same error.
Tls11 and Tls12
Might be that they have also stopped plain Tls as it is also vulnerable to POODLE attacks.
Can't easily go to TLS11 or TLS12 as program is running on WIN2003 server.

Tried some other things and actually got a response (not a good one) but got something back at least after changing the encoding from UTF-8 to UTF-16.

The error from the response is:         <Text>Unexpected Error: java.lang.NullPointerException</Text>

I am asking at the other end if anything has changed or if they can provide some feedback. Let me know if any other ideas.
ASKER CERTIFIED SOLUTION
Avatar of LordWabbit
LordWabbit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for getting back to this so late. Yeah it ended being an issue at their end.