【问题标题】:HTTPWebRequest 403 ForbiddenHTTPWebRequest 403 禁止
【发布时间】:2012-07-29 14:53:48
【问题描述】:

我正在尝试向服务器发送 HttpWebRequest。我已经测试了我正在使用的用户名和密码,并且他们使用目标网站上的登录表单正确地进行了身份验证。

我检查并阅读了 Stack 上关于 403 的所有其他帖子,其中涉及 HttpWebRequest,并且我更新了我的代码以具有有效的用户代理、Accept 字段和 ContentType。省略前三个属性之一似乎是人们最常犯的错误。

我的代码如下并打印“已创建响应远程服务器返回错误:(403) 禁止。”

{
    HttpWebRequest request = buildRequest(url);        
    print("Response Created");
    try
    {
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            //Do stuff with the response
        }
    }
    catch (Exception ex)
    {
        print(ex.Message);
    }
    Done.Visible = true;
}

private HttpWebRequest buildRequest(String url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    //I also tried authenticating in the header with no luck.
    //byte[] authBytes = Encoding.UTF8.GetBytes("username:password".ToCharArray());
    //req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
    req.Method = "GET";
    req.Credentials = new NetworkCredential("username","password");
    req.UserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-us) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27";
    req.PreAuthenticate = true;
    req.Accept = "application/json";
    req.ContentType = "application/json; charset=utf-8";
    req.KeepAlive = true;
    return req;
}

感谢您的帮助。

【问题讨论】:

    标签: c# httpwebrequest basic-authentication httpwebresponse


    【解决方案1】:

    您确定网站在登录时没有使用 POST 方法吗?如果没有,那么请忽略此答案。

    如果网站在提交数据时使用 POST 方法,您需要在提交之前将数据附加到请求的末尾(您可以使用 Fiddler 轻松检查):

    string postDataString = "foo1=bar1&foo2=bar2&foo3=bar3";
    //Use whatever encoding is appropriate for the submission
    byte[] postData = Encoding.ASCII.GetBytes(postDataString);
    
    
    //When building your request, set the ContentLength property and append the data
    req.ContentLength = postData.Length;
    
    using (Stream dataStream = req.GetRequestStream())
    {
        dataStream.Write(postData, 0, postData.Length);
    }
    

    【讨论】:

    • 感谢您的回复,但我知道该网站不使用 POST 方法来完成我正在尝试做的事情。我不是在尝试登录,而是在尝试验证 GET 命令以从服务器中获取一些 json。
    • 啊。您可能必须按照 rene 的建议走 cookie 路线。如果在使用网络浏览器时您必须先登录才能访问您的数据,那么您可能必须先通过您的程序登录才能获取 cookie。再说一次,如果它是不同的东西,那么不要介意我。只是提出一些建议:)
    【解决方案2】:

    使用System.Net.CookieContainer 在后续调用中接收和设置 cookie。 CookieContainer 属性在不同的请求中是有状态的。

    Here 是 MSDN 文档。

    public SomeClass 
    {
        var _cookies = new CookieContainer();
    
        private HttpWebRequest buildRequest(String url)  
        {  
           HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);  
           req.CookieContainer = _cookies;
           // omitted rest of code
        }
    
    }
    

    【讨论】:

    • 恐怕我没跟上。据我所知,我没有问任何关于 cookie 的问题。
    • 您是否使用fiddler2.com/fiddler2 通过普通浏览器和您的代码检查实际请求及其内容?
    • 是的,我应该在请求中查找任何具体内容吗?看起来我的请求包含编码的用户名/密码组合。
    • 正常的网络浏览器登录有多少请求和响应?它首先包括401吗?里面有响应 Cookies 吗?他们是否出现在下一次通话中?
    • 有 4 个响应,不包括 401,并且在任何调用中都没有 cookie。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2015-11-18
    • 2019-08-24
    • 2013-02-09
    • 2012-01-08
    • 2017-01-20
    • 2019-06-07
    相关资源
    最近更新 更多