[C#] 利用HttpWebRequest及HttpWebRequest製作網路爬蟲

[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);

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

What is 10 + 2 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)