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