我正在尝试以编程方式向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?
编辑:
我注意到我的请求包含
Expect
值为100的头继续,而浏览器的头不继续。我设置了
request.Expect
-属性到
null
并且
""
. 但我就是无法摆脱它。有什么建议吗?这可能是万恶之源吗?
编辑:
最后我移除了
期待
-页眉。但没用。有什么想法吗?
我通过设置激活了CookieContainer
request.CookieContainer = new CookieContainer();
但我在HTTP跟踪中看不到cookie头。为什么?