星期一, 11月 23, 2009

C# WebClient Post

使用webclient來做post,
很方便使用。
要能正確取得post的結果,
就要看各個網站的驗證的方法如何了。
方法如下:
System.Net.WebClient wc = new System.Net.WebClient();

string url = "http://www.thsrc.com.tw/tc/ticket/tic_time_result.asp";
string postData = "rom=1&to=5&sDate=2009%2F11%2F23&TimeTable=21%3A00&FromOrDest=From&x=33&y=10";

wc.Encoding = Encoding.UTF8;

byte[] sendData = Encoding.GetEncoding("utf-8").GetBytes(postData.ToString());

wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-TW; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5");
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.Headers.Add("Referer", "http://www.thsrc.com.tw/tc/index.asp");

byte[] recData = wc.UploadData(url, "POST", sendData);
string tmp = Encoding.UTF8.GetString(recData);



如果又要讓Session能夠保存的話,
可參考這篇的做法:
C# 實現Webclient保持Session的方法

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails