我收到“远程服务器返回错误:(422)无法处理的实体。”尝试发布帖子以登录网站时。我正在使用的代码如下所示。

    var cookieContainer = new CookieContainer();
    string postData = "authenticity_token=KE%2FfH4B6rQEMZJE2iTvwz3ZvqZfCkrCKvNTmWyN6NAg%3D&user%5Blogin%5D=XXXXX&user%5Bpassword%5D=XXXX&cookie%5Bremember_me%5D=off&commit=Login";
    ServicePointManager.Expect100Continue = false;
    var req = (HttpWebRequest)WebRequest.Create("https://www.assembla.com/user/do_login");
    req.Referer = "https://www.assembla.com/user/login";
    req.CookieContainer = cookieContainer;
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";
    byte[] bytes = Encoding.ASCII.GetBytes(postData);
    req.ContentLength = bytes.Length;
    Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length); //Push it out there
    os.Close();
    WebResponse resp = req.GetResponse();
    var sr = new StreamReader(resp.GetResponseStream());
    string s = sr.ReadToEnd().Trim();


通过浏览器的原始工作请求并给出http 302响应,如下所示。

POST https://www.assembla.com/user/do_login HTTP/1.1
Host: www.assembla.com
Connection: keep-alive
Referer: https://www.assembla.com/user/login
Content-Length: 179
Cache-Control: max-age=0
Origin: https://www.assembla.com
Content-Type: application/x-www-form-urlencoded
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,sv;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: __utmz=171517232.1296677998.7.6.utmcsr=assembla.com|utmccn=(referral)|utmcmd=referral|utmcct=/spaces/XXXX; assembla_sso=7E4FE565B34B069686F212063603EBF3%3AE6955C612241798CE408896D7AC27131FACE31CBB9D0CA68D9CF12DA463BBC16B74E28BB6C9E9C0141D90AC82EAB0766; _breakout_session=3d5a1d3d8abdc6cf277a6286efce82d5; __utma=171517232.1590472901.1291631853.1296677998.1296682201.8; __utmc=171517232; __utmv=171517232.|1=Logged=false=1,; __utmb=171517232.9.10.1296682201

authenticity_token=or3VG5%2Bon0ySRBZjTn7Ja1OU7LlFLjtXvr4JDth21Bo%3D&user%5Blogin%5D=XXXXX&user%5Bpassword%5D=XXXXX&cookie%5Bremember_me%5D=off&commit=Login


给出错误并使用上面的代码发送并给出http 422响应的请求如下所示。

POST https://www.assembla.com/user/do_login HTTP/1.1
Referer: https://www.assembla.com/user/login
Content-Type: application/x-www-form-urlencoded
Host: www.assembla.com
Content-Length: 179
Connection: Keep-Alive

authenticity_token=KE%2FfH4B6rQEMZJE2iTvwz3ZvqZfCkrCKvNTmWyN6NAg%3D&user%5Blogin%5D=XXXXX&user%5Bpassword%5D=XXXX&cookie%5Bremember_me%5D=off&commit=Login


还需要什么?我以为就足够了..?

小小的免责声明:如您所见,我尝试登录到https://www.assembla.com/user/login。就像该站点是https并具有简单的登录屏幕一样,这是一种测试。无不良意图。

最佳答案

这篇文章可能会对您有所帮助。他必须先点击登录页面以获取一些cookie,然后以实际的登录方法提交它们:

HttpWebRequest response produces HTTP 422. Why?

10-05 19:53