Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cannot Send HTTP Request in Windows Service

Posted on 2014-12-11
10
Medium Priority
?
567 Views
Last Modified: 2015-03-19
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
Comment
Question by:mathew_s
  • 5
  • 4
10 Comments
 
LVL 11

Expert Comment

by:LordWabbit
ID: 40495537
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
 
LVL 4

Expert Comment

by:Sathish DV
ID: 40495592
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
 
LVL 1

Author Comment

by:mathew_s
ID: 40496155
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 11

Expert Comment

by:LordWabbit
ID: 40496162
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
 
LVL 11

Expert Comment

by:LordWabbit
ID: 40496170
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
 
LVL 1

Author Comment

by:mathew_s
ID: 40496257
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
 
LVL 11

Expert Comment

by:LordWabbit
ID: 40496650
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
 
LVL 1

Author Comment

by:mathew_s
ID: 40496908
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
 
LVL 11

Accepted Solution

by:
LordWabbit earned 2000 total points
ID: 40497152
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
 
LVL 1

Author Comment

by:mathew_s
ID: 40677129
Sorry for getting back to this so late. Yeah it ended being an issue at their end.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
SSL is a very common protocol used these days when browsing the web.  The purpose is to provide security to communication, but how does it do it?  There are several pieces at work that have to be setup before SSL will even work and it requires both …
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
Suggested Courses
Course of the Month6 days, 8 hours left to enroll

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question