ASP.NET 4.0麻烦与ASP.NET HttpWebRequest的POST到网站
需要一些帮助,这令人烦恼的HTTP POST的问题 - 我已经看过#2,但无济于事其他职位。
问题摘要:这是一个经典的案例 - 我要登录到这需要2个参数登录外部网站,我需要使用POST做
会发生什么:我做一个POST和返回的HTTP响应基本上是我首先发布到的页面(即它没有真正登录)
我做过的事情:我有fiddler(协议分析器)正在运行,并且我的POST和一个工作POST(来自另一个桌面应用程序)的比较,bu牛逼我似乎无法重现相同的行为
[编辑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,但响应文本是相同的页面我张贴 - 即登录页面,提示我的帖子实际上并没有成功。
现在,这里是比较帖子
一个)FROM MY APPLICATION(不工作)
POST https://[someURL] 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(不同的桌面APP做同样的事情)
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[TRUNCATED]jF%2FLkgCgAA&__VIEWSTATE0=3&__VIEWSTATE=&__VIEWSTATE=¶m1=value1¶m2=value2&x=0&y=0
正如你所看到的,第二个是POST大很多 - 关键differenc ES是
- 有一些饼干,而我的不显示任何
- 的POST标题显示这些额外的字段(SomeField,VIEWSTATE1等) - 我如何访问它们做的一样吗?
难道我首先要做一个GET,解析身体,提取这些VIEWSTATE1--3 & SomeField,然后在后体数据字节重置呢?我可以尝试很多事情,但它会非常感激,如果有人告诉我,如果我的方式偏离轨道或正在做某种根本性错误......
我打算使用HTML敏捷包解析HTML
谢谢了一堆, g
我认为这是封闭式的,因为我超出了问题的范围。看起来我的问题与cookies无关,但是别的东西! (不知道还有什么......斗争仍在继续)。 – Gerry 2010-08-14 16:39:50
查看我的新帖http://stackoverflow.com/questions/3484558/vexed-post-returns-a-302-found-object-moved-error-in-httpwebrequest继续故事 – Gerry 2010-08-14 18:18:11
看到我的帖子在上面的评论为解决方案 – Gerry 2010-08-15 08:05:03