System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a receive.'
System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a receive.'
有人可以帮我写下面的代码吗?我有一个功能,我试图使用 URL "https://www1.nseindia.com/live_market/dynaContent/live_analysis/pre_open/all.json".
从网站获取一些数据
但出于某种原因,我总是收到 System.Net.WebException“'The underlying connection was closed: An unexpected error occurred on a receive.'”
我也可以使用 URL“https://www.nseindia.com/api/market-data-pre-open?key=ALL”获得相同的数据,但在这里我再次使用 C#.net 代码时得到相同的 WebException。
以下是我的代码:
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;
}
我尝试了 HTTPWebRequest 的不同变体以及不同的参数,但没有成功。在每种情况下,我要么得到相同的异常,要么 "The remote server returned an error: (403) Forbidden."
以下是我尝试过的选项:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive
非常感谢任何帮助...
您只需要摆脱 httpWebRequest.UserAgent 然后一切似乎都工作正常,因为 Http 请求不需要它。
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;
}
有人可以帮我写下面的代码吗?我有一个功能,我试图使用 URL "https://www1.nseindia.com/live_market/dynaContent/live_analysis/pre_open/all.json".
从网站获取一些数据但出于某种原因,我总是收到 System.Net.WebException“'The underlying connection was closed: An unexpected error occurred on a receive.'”
我也可以使用 URL“https://www.nseindia.com/api/market-data-pre-open?key=ALL”获得相同的数据,但在这里我再次使用 C#.net 代码时得到相同的 WebException。
以下是我的代码:
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;
}
我尝试了 HTTPWebRequest 的不同变体以及不同的参数,但没有成功。在每种情况下,我要么得到相同的异常,要么 "The remote server returned an error: (403) Forbidden."
以下是我尝试过的选项:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive
非常感谢任何帮助...
您只需要摆脱 httpWebRequest.UserAgent 然后一切似乎都工作正常,因为 Http 请求不需要它。
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;
}