0

I am trying to call a WCF service using HttpWebRequest. When I call it the first time, I get this error:

The underlying connection was closed: An unexpected error occurred on a send

If I call the same URL again, it is successful. Is there a reason why this call would cause an error the first time - and then it works?

This is my C# code to make the WCF call:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<URL>");
request.Timeout = 200000;
request.Method = method;
request.UserAgent = "Foo";
request.Accept = "*/*";
request.MaximumAutomaticRedirections = 4;
request.KeepAlive = false;
request.AllowAutoRedirect = false;
request.ContentLength = 0;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls 
                                        | SecurityProtocolType.Tls11 
                                        | SecurityProtocolType.Tls12 
                                        | SecurityProtocolType.Ssl3;
HttpWebResponse loWebResponse = (HttpWebResponse)request.GetResponse();
xdtTransform
  • 1,986
  • 14
  • 34
Sunil
  • 111
  • 1
  • 1
  • 6
  • Do you have the same issue when you add a reference to the service? Are you doing Soap or Rest? Can you share the binding, behavior, contract, and endpoint configuration? (you can hide the url but binding and security are nice). Is there a Password/ Certification call back ? – xdtTransform Dec 08 '20 at 07:59
  • I think this may help [C# HttpWebRequest: _The underlying connection was closed: An unexpected error occurred_](https://stackoverflow.com/questions/32788409/c-sharp-httpwebrequest-the-underlying-connection-was-closed-an-unexpected-error/32789483#32789483) – xdtTransform Dec 08 '20 at 08:02
  • The issue might came from Ssl3 that need a certificate validation call back. But you may not need to do all this by hands. And a simple service reference might just solve the all thing. – xdtTransform Dec 08 '20 at 08:05
  • Can you view specific exception information? We usually do not create an http request by ourselves to call the WCF service, but generate a proxy class based on the WSDL document of the WCF service to call the WCF service. – Ding Peng Jan 19 '21 at 08:09

0 Answers0