当我访问
时response = request.GetResponse();// request is a HttpWebRequest's object
远程服务器返回错误:(403) Forbidden. error
。但是当我在浏览器URL中执行请求中的url时,它会被重定向到我指定的页面。
这是完整的代码。
string newPostLink = string.Format("https://instagram.com/oauth/authorize/?client_id={0}&redirect_uri=http://localhost:2163/Main.aspx&response_type=code&scope=basic", clientId);
HttpWebRequest request =(HttpWebRequest) WebRequest.Create(newPostLink);
request.AllowAutoRedirect = false;
request.Referer = newPostLink;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";
request.Accept = "/";
request.UseDefaultCredentials = true;
request.Credentials = CredentialCache.DefaultCredentials;
if (request.Proxy != null)
{
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
}
var response = request.GetResponse();
答案 0 :(得分:1)
这可能与您在WebRequest中发送的UserAgent字符串有关。 您可以尝试更改UserAgent字符串以模拟该请求来自Firefox以查看它是否有效。