[C#] 利用HttpWebRequest及HttpWebRequest製作網路爬蟲 @ 大海男兒要出征 :: 痞客邦 PIXNET ::
[C#] 利用HttpWebRequest及HttpWebRequest製作網路爬蟲//準備data
CookieContainer cookieContainer = new CookieContainer();
string URI = “https://myinfo.tcu.edu.tw/login_check.asp";
string referer = “https://myinfo.tcu.edu.tw/login.asp";
string postString = “username=user&password=passw@rd";
byte[] postData = Encoding.ASCII.GetBytes(postString);
//設定相關參數
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);
request.Method = “POST";
request.AllowAutoRedirect = false;
//偽裝
request.Referer = referer;
request.ContentType = “application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
//送出httprequest資料流
Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
//取得httpresponse資料流
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding(“big5″));
string srcString = reader.ReadToEnd();
//輸出header資訊
Response.Write(response.Headers[HttpResponseHeader.Location]);
//輸出內文
Response.Write(srcString);