gpt4 book ai didi

c# - HttpWebRequest 响应生成 HTTP 422。为什么?

转载 作者:行者123 更新时间:2023-11-30 15:10:50 25 4
gpt4 key购买 nike

我正在尝试以编程方式向网络服务器发送 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 header ,而浏览器没有。我将 request.Expect 属性设置为 null""。但我就是无法摆脱它。有什么建议么?这可能是万恶之源吗?

编辑:

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

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

但我在 HTTP 跟踪中看不到 Cookie header 。为什么?

最佳答案

好的,伙计们,我知道了。

我有两个问题。

  1. HTTP 1.1/100 继续

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

    System.Net.ServicePointManager.Expect100Continue = false;
  2. 服务器响应 422因为一个与 cookies 。 POST 请求需要传输一个 cookie 容器,即包含一个 cookie 与当前 session 编号。如果这个 session ID 是不传输,服务器将用 422 响应。为了能够有一个带有 session ID 的 cookie,我必须在登录页面上执行一个简单的 HTTP 请求。该请求返回了一个带有所需 session ID 的 cookie 容器。然后我将返回的 cookie 容器传递给 POST 请求。

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

通过此设置,POST 请求可以成功发送。

感谢您的帮助。

关于c# - HttpWebRequest 响应生成 HTTP 422。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2938330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com