我想在
中获取HTML输出但我总是得到
HTTPWEBRESPONSE远程服务器返回错误:(403)Forbidden
我正在使用HttpWebResponse
protected string GetHtmlStringA(string url)
{
string sHtml = "";
HttpWebRequest request;
HttpWebResponse response = null;
Stream stream = null;
request = (HttpWebRequest)WebRequest.Create(url);
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default);
sHtml = sr.ReadToEnd();
if (stream != null) stream.Close();
if (response != null) response.Close();
return sHtml;
}
我也尝试使用UserAgent,但它是相同的
req.request=
"Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13";
我在论坛或互联网上找不到任何解决方案
答案 0 :(得分:7)
您似乎还需要发送Accept
标头。使用以下标头发送请求将起作用:
request.UserAgent = "Foo";
request.Accept = "*/*";
答案 1 :(得分:1)
您需要使用Web请求传递身份验证凭据:
request.Credentials = new NetworkCredentials("username", "password");
答案 2 :(得分:1)
确保正确设置了凭据。
request.Credentials = CredentialCache.DefaultCredentials;
// if we have a proxy set its creds as well
if( request.Proxy != null )
{
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
}
如果您需要特定凭据,可以通过这种方式创建
request.Credentials = new NetworkCredentials("username", "password");
答案 3 :(得分:0)
对我而言,就像他们不希望你屏蔽他们的内容一样。
你可以尝试用fiddler查看请求/响应,看看发生了什么。