Search code examples
c#httpwebrequestsystem.net.webexception

System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a receive.'


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 "https://www1.nseindia.com/live_market/dynaContent/live_analysis/pre_open/all.json".

But for some reason I am always getting the System.Net.WebException "'The underlying connection was closed: An unexpected error occurred on a receive.'"

Same data I can get using the URL "https://www.nseindia.com/api/market-data-pre-open?key=ALL" also, but here again I am getting the same WebException when using C#.net code.

Following is my 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;
        }

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."

Following are the options I tried:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive

C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send

any help is deeply appreciated...


Solution

  • You just need to get rid of the httpWebRequest.UserAgent and then everything seems to be working fine as the Http request doesnt require it.

    public static string GetNSEData()
        {
    
            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";
    
            string file;
            var response = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                file = sr.ReadToEnd();
            }
            return file;
        }