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

时间:2010-05-30 11:08:23

标签: c# .net post httpwebrequest

我正在尝试以编程方式向网络服务器发送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?

编辑:

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

编辑:

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

激活了CookieContainer
request.CookieContainer = new CookieContainer();

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

2 个答案:

答案 0 :(得分:4)

好的我已经有了它们。

我有两个问题。

  1. HTTP 1.1 / 100继续

    我通过设置

    解决了这个问题
    System.Net.ServicePointManager.Expect100Continue = false;
    
  2. 服务器用422重新编写 因为有关的问题 饼干。 POST请求需要 传输一个cookie容器,那个 包含当前的cookie 会话ID。如果此会话ID是 没有传输,服务器将 为了能够拥有会话ID的cookie,我必须在登录页面上执行简单的HTTP请求。此请求返回了具有所需会话ID的cookie容器。然后我将返回的cookie容器传递给POST请求。

    //cookie container of previous request
    postRequest.CookieContainer = cookieContainer; 
    
  3. 使用此设置可以成功发送POST请求。

    感谢您的帮助。

答案 1 :(得分:1)

您似乎没有正确地转义=和@。不确定这是否是唯一的问题。

您可以尝试HttpUtility.UrlEncode