HttpWebRequest响应产生HTTP 422.为什么?

我正在尝试以编程方式向Web服务器发送POST请求,以便登录然后执行其他需要登录的请求。

这是我的代码:

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"); 

发送请求后

  //Send post request var requestStream = request.GetRequestStream(); requestStream.Write(data, 0, data.Length); requestStream.Flush(); requestStream.Close(); 

…我想获得服务器响应:

  //Get Response StreamReader responseStreamReader = new StreamReader( request.GetResponse().GetResponseStream()); //WebException: HTTP 422! string content = responseStreamReader.ReadToEnd(); 

这段代码触发WebException,告诉我服务器响应HTTP 422 (由于语义错误导致的不可处理的实体)

然后我比较(使用TCP / IP嗅探器)我的程序和浏览器的请求(当然产生有效的POST请求并获得正确的响应)。

(1)我的计划要求:

 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)浏览器的请求:

  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?

编辑:

我注意到我的请求包含一个值为100-continue的Expect标头,而浏览器没有。 我将request.Expect -property设置为null并设置为"" 。 但我无法摆脱它。 有什么建议? 愿这成为万恶之源吗?

编辑:

最后我删除了Expect -Header。 但它没有帮助。 有任何想法吗? 我通过设置激活了CookieContainer

 request.CookieContainer = new CookieContainer(); 

但我无法在HTTP跟踪中看到Cookie-Header。 为什么?

好的我已经有了它们。

我有两个问题。

  1. HTTP 1.1 / 100继续

    我通过设置解决了这个问题

     System.Net.ServicePointManager.Expect100Continue = false; 
  2. 由于与cookie相关的问题,服务器重新使用422。 POST请求需要传输cookie容器,其中包含具有当前会话ID的cookie。 如果未传输此会话ID,则服务器将以422响应。为了能够拥有具有会话ID的cookie,我必须在登录页面上执行简单的HTTP请求。 此请求返回了具有所需会话ID的cookie容器。 然后我将返回的cookie容器传递给POST请求。

     //cookie container of previous request postRequest.CookieContainer = cookieContainer; 

使用此设置,可以成功发送POST请求。

谢谢你的帮助。

你似乎没有正确地逃避=和@。 不确定这是否是唯一的问题。

你可以尝试HttpUtility.UrlEncode 。