0

I have this code to send Http Get Request to https URL but I am getting this error:

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

var webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Timeout = timeout;//timeout 100 sec by default

            using (var httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
            using (var stream = httpWebResponse.GetResponseStream())
            using (var streamReader = new StreamReader(stream))
            {
                var response = streamReader.ReadToEnd();
                return response;
            }

The URL is working fine from browser and postman.

ahmed
  • 21
  • 1
  • 10
  • which line is giving this error? – Rakesh Burbure Aug 07 '18 at 06:00
  • Possible duplicate of https://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-connection-an-existing-connection-was-f – er-sho Aug 07 '18 at 06:01
  • @RakeshBurbure using (var httpWebResponse = (HttpWebResponse)webRequest.GetResponse()) – ahmed Aug 07 '18 at 06:06
  • try to set `webRequest.KeepAlive=False` and `WebRequest.ProtocolVersion = HttpVersion.Version10` and `WebRequest.ServicePoint.ConnectionLimit = 1` – er-sho Aug 07 '18 at 06:09
  • add this line just above to create web request and try `System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;` – er-sho Aug 07 '18 at 06:16
  • @RakeshBurbure that didn't work. Still giving the same error. – ahmed Aug 07 '18 at 06:22
  • did u add above `SecurityProtocol` line given above comment ? – er-sho Aug 07 '18 at 06:25
  • What is the timeout value set? please note by default it will be in milliseconds. – Rakesh Burbure Aug 07 '18 at 06:26
  • This code is working fine at my end. Please make sure something is not blocking in the service which you are calling. – Rakesh Burbure Aug 07 '18 at 06:28
  • I added it ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls . It is accepting TLS only not Tls11 or Tls12 – ahmed Aug 07 '18 at 06:29
  • timeout is 100000 milliseconds – ahmed Aug 07 '18 at 06:29
  • use above all three `Tls`, `Tls11`, `Tls12` at once – er-sho Aug 07 '18 at 06:30
  • just copy `System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;` this and paste in your code as it is – er-sho Aug 07 '18 at 06:31
  • @ahmed, Is above comment code working fine? – er-sho Aug 07 '18 at 06:48
  • @ershoaib yes it worked. My problem now is to make it work on .Net Framework 4.0 as TLS1.2 is not supported so instead of that line I am using System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)3072; but it is not working – ahmed Aug 07 '18 at 06:50
  • use this `ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;` – er-sho Aug 07 '18 at 06:58
  • yes, now it is working. Thanks, @ershoaib RakeshBurbure – ahmed Aug 07 '18 at 07:33
  • now i add this as answer you then accept it – er-sho Aug 07 '18 at 07:34

1 Answers1

3

Try to add this code just above your WebRequest.Create(url);

If you are using .Net Framework 4.0

System.Net.ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;

If you are using .Net Framework 4.5 and above

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
er-sho
  • 9,581
  • 2
  • 13
  • 26