У меня есть этот код:
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-й вызовы не работают в тот же экземпляр.
Может кто-нибудь, пожалуйста, помогите мне с этим ...
Заранее спасибо.