c# - 具有多个 URL 的 HttpWebResponse 导致错误
问题描述
我有这个代码:
public class WebDataDownloader
{
public string GetJSONFromURL(string url)
{
string file = string.empty;
//*********get the json file using httpRequest ***********
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
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.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using (var response = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (var sr = new StreamReader(response.GetResponseStream()))
{
file = sr.ReadToEnd();
}
}
}
catch(Exception exp)
{
Debug.WriteLine(exp.Message);
}
return file;
}
}
现在我多次调用这个方法来获取 JSON 并解析它。如下所示:
public class MyClass
{
WebDataDownloader myWebClient = new WebDataDownloader();
string Json1 = myWebClient.GetJSONFromURL(@"https://www.nseindia.com/api/market-data-pre-open?key=ALL");
//Do some action on this data
string Json2 = myWebClient.GetJSONFromURL(@"https://www.nseindia.com/api/equity-stock?index=fu_nifty50");
//Do some action on this data
string Json3 = myWebClient.GetJSONFromURL(@"https://www.nseindia.com/api/liveEquity-derivatives?index=nifty_bank_fut");
//Do some action on this data
}
但是,问题是,第一次调用 (Json1) 使用
response = (HttpWebResponse)httpWebRequest.GetResponse()
工作正常,但第 2 次和第 3 次调用在同一实例中不起作用。
有人可以帮我解决这个问题吗...
提前致谢。
解决方案
推荐阅读
- flutter - 如何用颤振计算列表字符串中的相同日期?
- java - 如何清除 Apache Tomcat 日志?
- directx - Windows 10 屏幕截图 DXGI 截图
- ios - 如何在 ios 目标 c 中创建嵌套滚动视图?
- python-3.x - 当我的电报机器人离线(非轮询)时,我可以接收发送到我的电报机器人的消息吗?
- regex - 如何删除大括号内的代码块?
- c# - 使用 Asp 标签导航到另一个 Html 页面 .net core mvc
- c# - 在初始化 aspnetcore 应用程序本身之前,如何在 aspnetcore 应用程序中设置我的日志记录服务
- node.js - 如何在 sequelize 中更新事务中的关联模型
- c - 如何用 Frama-C 证明 C stringCompare 函数的功能?