• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • Last Modified:

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.
0
mathew_s
Asked:
mathew_s
  • 5
  • 4
1 Solution
 
LordWabbitCommented:
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?
0
 
Sathish DVSoftware EngineerCommented:
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.
0
 
mathew_sAuthor Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LordWabbitCommented:
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.
0
 
LordWabbitCommented:
So changing
   ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

Open in new window

to
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

Open in new window

should sort out the error.
0
 
mathew_sAuthor Commented:
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
0
 
LordWabbitCommented:
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.
0
 
mathew_sAuthor Commented:
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.
0
 
LordWabbitCommented:
Well other than verifying what you already surmise, this error being thrown is on the service side and it appears to be written in java.  BUT it could be caused by you making a bad request, is there any way you can get them to test their service through their own firewall with ssl enabled?  A depressing number of companies I have worked with 'test' their services from within their own network, instead of through the myriad of security layers external companies have to wade through.  There is also the disconnect (ie. hostility) between networks and software which is prevalent in every company I have worked in.  Networks change things around for whatever reason  (without communicating because they think they are gods) and software starts failing, and the end user is affected and as far as the end user is concerned it's a software problem.

Anyways, a different error is better than the same error, gives you something to work with or rule out.
0
 
mathew_sAuthor Commented:
Sorry for getting back to this so late. Yeah it ended being an issue at their end.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now