c# - 远程服务器返回错误 : (403) Forbidden in Asp.net C# Instagram.(HttpWebRequest)

标签 c# asp.net httpwebrequest webclient instagram

当我访问

response = request.GetResponse();// request is a HttpWebRequest's object

远程服务器返回错误:(403) Forbidden。错误。但是当我在我的浏览器 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();

最佳答案

这可能与您在 WebRequest 中发送的 UserAgent 字符串有关。 您可以尝试更改 UserAgent 字符串以模拟该请求来自 Firefox,然后查看它是否有效。

关于c# - 远程服务器返回错误 : (403) Forbidden in Asp.net C# Instagram.(HttpWebRequest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20799287/

相关文章:

c# - String.Split() 返回空值

c# - 构建通用过滤器参数输入接口(interface)?

c# - 如何在 Razor mvc5 中每第 3 列创建一行?

asp.net - Elmah 不保存错误

c# - 在 Reactive Extensions 中一次处理一个事件

c# - 检查图像是否存在于我的本地资源中

javascript - 如何检查面板在 JavaScript 中是否可见?

HttpWebRequest ReadWriteTimeout 在 .NET 中被忽略;在单声道工作

如果 URL 中没有 HTTP,则 SQL 相关错误

c# - 如何使用异步 Web 请求进行多线程处理