【发布时间】:2011-03-29 19:20:28
【问题描述】:
ASP.NET 4.0
在这个棘手的 HTTP POST 问题上需要一些帮助 - 我查看了 Stackoverflow 上的其他帖子,但无济于事。
问题总结:这是一个经典案例——我想登录一个需要2个参数登录的外部站点,我需要使用POST来完成它
会发生什么:我执行 POST 后,返回的 HTTP 响应基本上与我最初发布到的页面相同(即它还没有真正登录)
我做过的事情:我正在运行 fiddler(协议分析器),并且我比较了我的 POST 和一个正常工作的 POST(来自另一个桌面应用程序),但我似乎无法重现相同的行为
[edit 1]: 这似乎是一个 cookie 问题(下面的代码现在已经过时,我已经进行了修改),我已经设法正确设置了所有参数。截至本次编辑,问题尚未解决。
以下是我的代码,我还复制了比较标题
private static void doPost(string URL)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.126 Safari/533.4";
CookieContainer cCookie = new CookieContainer();
myRequest.CookieContainer = cCookie;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
string postData = "param1=somevalue¶m2=someothervalue";
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] bData = ascii.GetBytes(postData);
myRequest.Method = "POST";
myRequest.ContentLength = bData.Length;
Stream oStream = myRequest.GetRequestStream();
oStream.Write(bData, 0, bData.Length);
string oResp = string.Empty;
using (var resp = myRequest.GetResponse())
{
using (var responseStream = resp.GetResponseStream())
{
using (var responseReader = new StreamReader(responseStream))
{
oResp = responseReader.ReadToEnd();
}
}
}
Console.WriteLine(oResp);
}
我确实获得了 HTTP 1.1 OK,但响应文本与我发布到的页面相同 - 即登录页面,这表明我的帖子实际上并未成功。
现在这里是比较帖子
a) 来自我的应用程序(不起作用)
POST @987654321@ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.126 Safari/533.4
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
Host: xxxx.com
Content-Length: 64
Expect: 100-continue
Connection: Keep-Alive
param1=value1¶m2=value2
b) 来自工作的帖子(不同的桌面应用程序做同样的事情)
POST https:[someURL] HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, /
Referer: [someURL]
Accept-Language: en-US
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; InfoPath.2; .NET4.0C; .NET4.0E)
Host: xxxxxx.com
Content-Length: 2815
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: ccccc1=10; blahblah=pppqqqrrr; ASP.NET_SessionId=jp4mjg45z34si545om3nouew
SomeField=False&__VIEWSTATE1=1H4sIAA3mnn8A%2F31WTbequfx[截断]jF%2FLkgCgAA&__VIEWSTATE0=3&__VIEWSTATE=&__VIEWSTATE=¶m1=value1¶m2=value2&x=0&y=0
如您所见,第二个 POST 更大 - 主要区别是
- 有一些 cookie,而我的没有显示任何
- POST 标头显示这些附加字段(SomeField、VIEWSTATE1 等) - 我如何访问它们以执行相同的操作?
我是否首先需要做一个 GET,解析正文,提取这些 VIEWSTATE1--3 & SomeField,然后在 post body 数据字节中重置它?我可以尝试很多事情,但如果有人告诉我我是否偏离轨道或做一些根本错误的事情,我将不胜感激......
我打算使用 Html Agility 包来解析 HTML
非常感谢, g
【问题讨论】:
-
我认为这是封闭的,因为我已经远远超出了问题的范围。看来我的问题也与 cookie 无关,而是其他问题! (还不知道是什么……斗争还在继续)。
-
查看我的新帖子 stackoverflow.com/questions/3484558/… 了解后续故事
-
解决方法见我上面评论中的帖子
标签: c# asp.net .net-3.5 post httpwebrequest