title: System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a receive.'
body: <p>Can somebody please help me with the below code. I have a function where I am trying to get some data from a website using the URL "<a href="https://www1.nseindia.com/live_market/dynaContent/live_analysis/pre_open/all.json" rel="nofollow noreferrer">https://www1.nseindia.com/live_market/dynaContent/live_analysis/pre_open/all.json</a>".</p>
  
  <p>But for some reason I am always getting the System.Net.WebException "'The underlying connection was closed: An unexpected error occurred on a receive.'"</p>
  
  <p>Same data I can get using the URL "<a href="https://www.nseindia.com/api/market-data-pre-open?key=ALL" rel="nofollow noreferrer">https://www.nseindia.com/api/market-data-pre-open?key=ALL</a>" also, but here again I am getting the same WebException when using C#.net code.</p>
  
  <p>Following is my code:</p>
  
  <pre><code>public static string GetNSEData()
          {
              //*********get the json file using httpRequest ***********
              HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www1.nseindia.com/live_market/dynaContent/live_analysis/pre_open/all.json");
              httpWebRequest.Method = WebRequestMethods.Http.Get;
              httpWebRequest.Accept = "application/json; charset=utf-8";
              httpWebRequest.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36";
              //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
              //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  
              string file;
              var response = (HttpWebResponse)httpWebRequest.GetResponse();
              using (var sr = new StreamReader(response.GetResponseStream()))
              {
                  file = sr.ReadToEnd();
              }
              return file;
          }
  </code></pre>
  
  <p>I have tried different variations of HTTPWebRequest along with different parameters but no success. In every case I am either getting the same exception or "The remote server returned an error: (403) Forbidden."</p>
  
  <p>Following are the options I tried:</p>
  
  <p><a href="https://stackoverflow.com/questions/751056/system-net-webexception-the-underlying-connection-was-closed-an-unexpected-err">System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive</a></p>
  
  <p><a href="https://stackoverflow.com/questions/29886223/c-sharp-system-net-webexception-the-underlying-connection-was-closed-an-unexpe">C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send</a></p>
  
  <p>any help is deeply appreciated...</p>