Solved

Cannot Send HTTP Request in Windows Service

Posted on 2014-12-11
10
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

We recently endured a series of broadcast storms that caused our ISP to shut us down for brief periods of time. After going through a multitude of tests, we determined that the issue was related to Intel NIC drivers on some new HP desktop computers …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
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…

739 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