Reputation: 11947
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.
Upvotes: 2
Views: 5861
Reputation: 55417
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?
Upvotes: 1