以下C#代码在DownloadString期间保持返回403:Forbidden。我正在笔记本电脑上进行单元测试中运行代码。当我将网址粘贴到浏览器(Chrome和IE)时,它可以正常运行。此外,当我删除clientID,传感器和签名时,它工作正常。
我尝试过添加标题,SecurityProtocol等许多变种无济于事。
网址如下:
其中## clientID ##和## signature ##是公司内部价值。
protected virtual string SendRequest(string url)
{
Console.WriteLine(url);
string response = null;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
using (WebClient client = new WebClient())
{
//client.Headers.Add("User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
client.Headers.Add("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
client.Headers.Add("Host", "maps.googleapis.com");
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8");
client.Headers.Add("Accept-Language", "en-US,en;q=0.5");
client.Headers.Add("Accept-Encoding", "gzip, deflate");
response = client.DownloadString(url);
Console.WriteLine(">>>" + response);
}
return response;
}
使用有效的httpfox,我看到以下内容:
没有饼干。
在httpfox的内容标签中: 加载内容时出错(NS_ERROR_DOCUMENT_NOT_CACHED)