我正在尝试以编程方式向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-trace中看不到Cookie-Header。为什么?
发布于 2010-05-31 02:05:10
好了,伙计们,我知道了。
我有两个问题。
我解决这个问题的方法是
System.Net.ServicePointManager.Expect100Continue =false;
//上一次请求的cookie容器postRequest.CookieContainer = cookieContainer;
通过此设置,可以成功发送POST请求。
谢谢你的帮助。
发布于 2010-05-30 19:16:12
您似乎没有正确地转义=和@。不确定这是不是唯一的问题。
你可以试试HttpUtility.UrlEncode。
https://stackoverflow.com/questions/2938330
复制相似问题