C# 프로그램 (nseindia.com)을 사용하여 사이트에서 데이터를 검색하는 데 사용한 적이 있지만 최근에는 NSE에서 프로그램의 모든 요청에 "403 Forbidden Error"로 응답하도록 일부 변경했습니다. 누구든지 프로그램에서 브라우저와 동일한 요청을하는 방법을 알려줄 수 있습니까? userAgent 속성을 설정하려고했지만 작동하지 않습니다. 아래에 코드가 붙여 넣어집니다.HttpWebRequest 대 브라우저 요청
string DownloadData(string CompanyName)
{
string address = string.Format(@"http://www.nseindia.com");
//http://www.nseindia.com/marketinfo/sym_map/symbolMapping.jsp?dataType=priceVolumeDeliverable&symbol=abb&
//http://www.nseindia.com/content/equities/scripvol/datafiles/01-12-2008-TO-29-12-2010ABBALLN.csv
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3
string strData = "";
try
{
request.Proxy = WebProxy.GetDefaultProxy();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();
System.Text.Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
System.IO.StreamReader reader = new System.IO.StreamReader(stream, ec);
strData = reader.ReadToEnd();
if (strData.Contains("Error"))
{
Exception e = new Exception(strData);
throw e;
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
return strData;
}
다른 웹 사이트 (예 : http://www.google.com)에서 다른 URL을 요청하려 했습니까? 어쩌면 기업 정책이 변경되어 프록시를 사용하지 않고 직접 http 요청을 할 수 없게 될 수도 있습니다. –
예. Google을 사용해 보았을 때 정상적으로 작동했습니다. 빠른 답장을 보내 주셔서 감사합니다. – Martin