在C#中,能够使用HttpWebRequest进行相关的模拟登录,登录后进行相关的操做,好比抓取数据,页面分析,制做相关登录助手等等。web
先说下流程c#
1.使用httpwebrequest先进入你要登陆的网站,获取cookie服务器
2.使用第一步获取的cookie到验证码的网页将验证码下载下来。cookie
3.使用Post数据 发送至网站。若是有cookie则继续保存。app
4.使用第三步的cookie登录相关网页操做。工具
获取相关数据能够使用抓包工具进行抓取,如httpwatch。(网上下载的好多都有病毒,下载的时候注意点)post
1。网站
- public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
- {
- HttpWebRequest request;
- HttpWebResponse response;
- ArrayList list = new ArrayList();
- request = WebRequest.Create(postUrl) as HttpWebRequest;
- request.Method = "GET";
- request.UserAgent = "Mozilla/4.0";
- request.CookieContainer = cookie;
- request.KeepAlive = true;
-
- request.CookieContainer = cookie;
- try
- {
-
- using (response = (HttpWebResponse)request.GetResponse())
- {
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
- {
- cookie.Add(response.Cookies);
-
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
- list.Add(Guid.NewGuid().ToString());
- }
- }
- }
- catch (WebException ex)
- {
- list.Clear();
- list.Add("发生异常/n/r");
- WebResponse wr = ex.Response;
- using (Stream st = wr.GetResponseStream())
- {
- using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
- catch (Exception ex)
- {
- list.Clear();
- list.Add("5");
- list.Add("发生异常:" + ex.Message);
- }
- return list;
- }
2.下载验证码,保存在本地。ui
-
-
-
-
-
- public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath)
- {
- bool bol = true;
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
-
- webRequest.AllowWriteStreamBuffering = true;
- webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
- webRequest.MaximumResponseHeadersLength = -1;
- webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
- webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
- webRequest.ContentType = "application/x-www-form-urlencoded";
- webRequest.Method = "GET";
- webRequest.Headers.Add("Accept-Language", "zh-cn");
- webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
- webRequest.KeepAlive = true;
- webRequest.CookieContainer = cookCon;
- try
- {
-
- using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
- {
- using (Stream sream = webResponse.GetResponseStream())
- {
- List<byte> list = new List<byte>();
- while (true)
- {
- int data = sream.ReadByte();
- if (data == -1)
- break;
- list.Add((byte)data);
- }
- File.WriteAllBytes(savePath, list.ToArray());
- }
- }
- }
- catch (WebException ex)
- {
- bol = false;
- }
- catch (Exception ex)
- {
- bol = false;
- }
- return bol;
- }
3。发送post数据url
- public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie)
- {
- ArrayList list = new ArrayList();
- HttpWebRequest request;
- HttpWebResponse response;
- ASCIIEncoding encoding = new ASCIIEncoding();
- request = WebRequest.Create(postUrl) as HttpWebRequest;
- byte[] b = encoding.GetBytes(postData);
- request.UserAgent = "Mozilla/4.0";
- request.Method = "POST";
- request.CookieContainer = cookie;
- request.ContentLength = b.Length;
- using (Stream stream = request.GetRequestStream())
- {
- stream.Write(b, 0, b.Length);
- }
-
- try
- {
-
- using (response = request.GetResponse() as HttpWebResponse)
- {
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
- {
- if (response.Cookies.Count > 0)
- cookie.Add(response.Cookies);
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
- }
- }
- }
- catch (WebException wex)
- {
- WebResponse wr = wex.Response;
- using (Stream st = wr.GetResponseStream())
- {
- using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
- catch (Exception ex)
- {
- list.Add("发生异常/n/r"+ex.Message);
- }
- return list;
- }
4。就是第三步请求的连接地址换一个就好了
好了
以上核心代码已经贴出了
具体实现须要靠大家按照大家本身的逻辑
还有一些header能不写就不写,由于我2天前一直在获取返回response这地方报500错误。
找了N多代码,看了N多资料都不能够。最后将一些header注释掉就能够了,真郁闷。