HttpWebRequest 帖子中返回 422 错误
当我尝试发帖登录网站时,收到“远程服务器返回错误:(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 的并且有一个简单的登录屏幕。没有恶意。
I'm getting a "The remote server returned an error: (422) Unprocessable Entity." when trying to do a post to login to a site. The code I'm using look something like below.
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();
The raw working request via the browser and gives a http 302 response looks like below.
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
The request that gives the error and that is send using the code above and gives a http 422 response look like this.
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
What else is needed? I thought this would be enough ..?
Tiny disclaimer: As you can see I try to login to https://www.assembla.com/user/login. This is just as a test as this site was https and had a simple login screen. No bad intension.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这篇文章可能对你有帮助。他必须先点击登录页面来获取一些cookie,然后在实际的登录方法中提交它们:
HttpWebRequest 响应产生 HTTP 422。为什么?
This post might help you. He had to hit the login page first to pick up some cookies and then submit them in the actual login method:
HttpWebRequest response produces HTTP 422. Why?