HttpWebRequest的响应,产生HTTP 422为什么呢? [英] HttpWebRequest response produces HTTP 422. Why?
问题描述
我试图以编程方式发送一个POST请求到Web服务器才能登录的,然后执行需要登录的其他请求。
I'm trying to programmatically send a POST-request to a web-server in order to login an then perform other requests that require a login.
这是我的code:
byte[] data = Encoding.UTF8.GetBytes(
String.Format(
"login={0}&password={1}&authenticity_token={2}"
+"&login_submit=Entra&remember_me=1",
HttpUtility.UrlEncode(username), HttpUtility.UrlEncode(password),
HttpUtility.UrlEncode(token)));
//Create HTTP-request for login
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create("http://www.xxx.xx/xx/xx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.CookieContainer = new CookieContainer();
request.Accept = "application/xml,application/xhtml+xml,text/html;
+"q=0.9,text/plain ;q=0.8,image/png,*/*;q=0.5";
request.Referer = "http://www.garzantilinguistica.it/it/session";
request.Headers.Add("Accept-Language", "de-DE");
request.Headers.Add("Origin", "http://www.xxx.xx");
request.UserAgent = "C#";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
在发送请求后
After sending the request
//Send post request
var requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Flush();
requestStream.Close();
......我想获得服务器的响应:
... I want to get the servers response:
//Get Response
StreamReader responseStreamReader = new
StreamReader(
request.GetResponse().GetResponseStream()); //WebException: HTTP 422!
string content = responseStreamReader.ReadToEnd();
这一块code类火灾的WebException,告诉我该服务器的 HTTP 422 (由于语义错误不能处理实体)
This piece of code fires the WebException, that tells me the server responded with HTTP 422 (unprocessable entity due to semantic errors)
然后我比较(使用的是TCP / IP嗅探器),我的程序的请求和浏览器(当然这会产生一个有效的POST请求,并得到正确的反应)。
Then I compared (using a TCP/IP sniffers) the requests of my program and the browser (which of course produces a valid POST-request and gets the right response).
(1)我的程序的请求:
(1) My program's request:
POST /it/session HTTP/1.1
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
Referer: http://www.garzantilinguistica.it/it/session
Accept-Language: de-DE
Origin: http://www.garzantilinguistica.it
User-Agent: Test
Accept-Encoding: gzip, deflate
Host: www.garzantilinguistica.it
Content-Length: 148
Expect: 100-continue
Connection: Keep-Alive
HTTP/1.1 100 Continue
login=thespider14%40hotmail.com&password=xxxxx&authenticity_token=4vLgtwP3nFNg4NeuG4MbUnU7sy4z91Wi8WJXH0POFmg%3d&login_submit=Entra&remember_me=1
(2)浏览器的请求:
(2) The browser's request:
POST /it/session HTTP/1.1
Host: www.garzantilinguistica.it
Referer: http://www.garzantilinguistica.it/it/session
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,
text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: de-DE
Origin: http://www.garzantilinguistica.it
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de-DE) AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Cookie: __utma=244184339.652523587.1275208707.1275208707.1275211298.2; __utmb=244184339.20.10.1275211298; __utmc=244184339; __utmz=244184339.1275208707.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _garzanti2009_session=BAh7CDoPc2Vzc2lvbl9pZCIlZDg4MWZjNjg2YTRhZWE0NDQ0ZTJmMTU2YWY4ZTQ1NGU6EF9jc3JmX3Rva2VuIjFqRWdLdll3dTYwOTVVTEpNZkt6dG9jUCtaZ0o4V0FnV2V5ZnpuREx6QUlZPSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsGOgplcnJvciIVbG9naW4gbm9uIHZhbGlkbwY6CkB1c2VkewY7CFQ%3D--4200fa769898dd156faa49e457baf660cf068d08
Content-Length: 144
Connection: keep-alive
authenticity_token=jEgKvYwu6095ULJMfKztocP%2BZgJ8WAgWeyfznDLzAIY%3D&login=thespider14%40hotmail.com&password=xxxxxx&remember_me=1&commit=Entra
HTTP/1.1 302 Found
有人可以帮助理解我失踪或者是该请求的一部分的主 与浏览器之间的差异我的要求?为什么我收到了422?
Can someone help to understand which part of the request I am missing or what the main difference between the browser's and my request is? Why am I getting that 422?
编辑:
我发现我的请求包含期望
与头值100继续,而浏览器则没有。我设置了 request.Expect
-property到空
和,
。但我就是无法摆脱它。有什么建议么?也许这是一切罪恶的根源?
I noticed that my request contains a Expect
header with the value 100-continue, whereas the browser's doesn't. I set the request.Expect
-property to null
and to ""
. But I just couldn't get rid of it. Any suggestions? May this be the root of all evil?
编辑:
最后,我删除了期望
-Header。但它并没有帮助。有任何想法吗?
我激活了的CookieContainer通过设置
Finally I removed the Expect
-Header. But it didn't help. Any ideas?
I activated the CookieContainer by setting
request.CookieContainer = new CookieContainer();
但我看不到这个Cookie标头在HTTP-跟踪。为什么呢?
But I can't see the Cookie-Header in the HTTP-trace. Why?
推荐答案
好,我知道了人。
我有两个问题。
-
HTTP 1.1 / 100继续
HTTP 1.1/ 100 continue
我解决这通过设置
System.Net.ServicePointManager.Expect100Continue = false;
服务器repsonded与422 由于涉及到的一个问题 曲奇饼。 POST请求需要 发送一个cookie的容器,这 包含与当前的cookie 会话ID。如果这个会话ID 不传输,服务器将 与422响应为了能够与会话ID的Cookie,我不得不进行登录页面上的一个简单的HTTP请求。该请求返回一个cookie容器所需的会话ID。然后,我通过返回的cookie的容器POST请求。
The server repsonded with 422 because of a problem related to the cookies. The POST request needs to transmit a cookie container, that contains a cookie with the current session id. If this session id is not transmitted, the Server will respond with 422. In order to be able to have a cookie with the session id, I had to perform a simple HTTP-request on the login page. This request returned a cookie container with the needed session id. Then I passed the returned cookie container to POST request.
//cookie container of previous request
postRequest.CookieContainer = cookieContainer;
通过此设置POST请求可以成功发送。
With this settings the POST request could be sent successfully.
感谢您的帮助。
这篇关于HttpWebRequest的响应,产生HTTP 422为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!