各位前輩 請教教小弟 如何用 C# HttpWebRequest 登入某個網頁 因為我看回傳的網頁碼都是登入頁面的程式碼
這是網頁網址 http://ag1.amctqk418.cdbybj.com:88/
string param = "loginName=" + loginname + "& loginPwd=" + loginpassword;
byte[] bs = Encoding.ASCII.GetBytes(param);
string URL = "http://ag1.amctqk418.cdbybj.com:88/";
HttpWebRequest request = HttpWebRequest.Create(URL) as HttpWebRequest;
request.Referer = "http://ag1.amctqk418.cdbybj.com:88/index.aspx";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
request.Headers.Set("Accept-Language", "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7");
//request.Headers.Set("Accept-Encoding", "gzip, deflate");
string result = null;
request.Method = "POST"; // 方法
request.KeepAlive = true; //是否保持連線
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = session;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
using (WebResponse response = request.GetResponse())
{
StreamReader srr = new StreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8);
result = srr.ReadToEnd();
cookie = request.CookieContainer.GetCookieHeader(request.RequestUri);
srr.Close();
}
Console.WriteLine(result);
我只有在reqStream.Write(bs, 0, bs.Length); 這裡面發現下面例外 我不知道有沒有影響
你需要的功能可能有點算高級爬蟲了,
上網搜尋一下 高級爬蟲 也許會找到你要的,
單純 HttpWebRequest 能夠做的比較有限.