2011-02-02 77 views
2

當您試圖發佈帖子登錄到站點時,我收到一個「遠程服務器返回了錯誤:(422)Unprocessable Entity。」。我使用的代碼如下所示。HttpWebRequest post中返回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,並有一個簡單的登錄屏幕。沒有不好的意思。

回答