分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报

在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等。

先说下流程

1.使用httpwebrequest先进入你要登录的网站,获取cookie

2.使用第一步获取的cookie到验证码的网页将验证码下载下来。

3.使用Post数据 发送至网站。如果有cookie则继续保存。

4.使用第三步的cookie登陆相关网页操作。

获取相关数据可以使用抓包工具进行抓取,如httpwatch。(网上下载的好多都有病毒,下载的时候注意点)

1。

  1. /// <summary>
  2. /// 通过get方式请求页面,传递一个实例化的cookieContainer
  3. /// </summary>
  4. /// <param name="postUrl"></param>
  5. /// <param name="cookie"></param>
  6. /// <returns></returns>
  7. public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
  8. {
  9. HttpWebRequest request;
  10. HttpWebResponse response;
  11. ArrayList list = new ArrayList();
  12. request = WebRequest.Create(postUrl) as HttpWebRequest;
  13. request.Method = "GET";
  14. request.UserAgent = "Mozilla/4.0";
  15. request.CookieContainer = cookie;
  16. request.KeepAlive = true;
  17. request.CookieContainer = cookie;
  18. try
  19. {
  20. //获取服务器返回的资源
  21. using (response = (HttpWebResponse)request.GetResponse())
  22. {
  23. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
  24. {
  25. cookie.Add(response.Cookies);
  26. //保存Cookies
  27. list.Add(cookie);
  28. list.Add(reader.ReadToEnd());
  29. list.Add(Guid.NewGuid().ToString());//图片名
  30. }
  31. }
  32. }
  33. catch (WebException ex)
  34. {
  35. list.Clear();
  36. list.Add("发生异常/n/r");
  37. WebResponse wr = ex.Response;
  38. using (Stream st = wr.GetResponseStream())
  39. {
  40. using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
  41. {
  42. list.Add(sr.ReadToEnd());
  43. }
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. list.Clear();
  49. list.Add("5");
  50. list.Add("发生异常:" + ex.Message);
  51. }
  52. return list;
  53. }

2.下载验证码,保存在本地。

  1. /// <summary>
  2. /// 下载验证码图片并保存到本地
  3. /// </summary>
  4. /// <param name="Url">验证码URL</param>
  5. /// <param name="cookCon">Cookies值</param>
  6. /// <param name="savePath">保存位置/文件名</param>
  7. public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath)
  8. {
  9. bool bol = true;
  10. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
  11. //属性配置
  12. webRequest.AllowWriteStreamBuffering = true;
  13. webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
  14. webRequest.MaximumResponseHeadersLength = -1;
  15. 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, */*";
  16. webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
  17. webRequest.ContentType = "application/x-www-form-urlencoded";
  18. webRequest.Method = "GET";
  19. webRequest.Headers.Add("Accept-Language", "zh-cn");
  20. webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
  21. webRequest.KeepAlive = true;
  22. webRequest.CookieContainer = cookCon;
  23. try
  24. {
  25. //获取服务器返回的资源
  26. using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
  27. {
  28. using (Stream sream = webResponse.GetResponseStream())
  29. {
  30. List<byte> list = new List<byte>();
  31. while (true)
  32. {
  33. int data = sream.ReadByte();
  34. if (data == -1)
  35. break;
  36. list.Add((byte)data);
  37. }
  38. File.WriteAllBytes(savePath, list.ToArray());
  39. }
  40. }
  41. }
  42. catch (WebException ex)
  43. {
  44. bol = false;
  45. }
  46. catch (Exception ex)
  47. {
  48. bol = false;
  49. }
  50. return bol;
  51. }

3。发送post数据

  1. /// <summary>
  2. /// 发送相关数据至页面
  3. /// 进行登录操作
  4. /// 并保存cookie
  5. /// </summary>
  6. /// <param name="postData"></param>
  7. /// <param name="postUrl"></param>
  8. /// <param name="cookie"></param>
  9. /// <returns></returns>
  10. public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie)
  11. {
  12. ArrayList list = new ArrayList();
  13. HttpWebRequest request;
  14. HttpWebResponse response;
  15. ASCIIEncoding encoding = new ASCIIEncoding();
  16. request = WebRequest.Create(postUrl) as HttpWebRequest;
  17. byte[] b = encoding.GetBytes(postData);
  18. request.UserAgent = "Mozilla/4.0";
  19. request.Method = "POST";
  20. request.CookieContainer = cookie;
  21. request.ContentLength = b.Length;
  22. using (Stream stream = request.GetRequestStream())
  23. {
  24. stream.Write(b, 0, b.Length);
  25. }
  26. try
  27. {
  28. //获取服务器返回的资源
  29. using (response = request.GetResponse() as HttpWebResponse)
  30. {
  31. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  32. {
  33. if (response.Cookies.Count > 0)
  34. cookie.Add(response.Cookies);
  35. list.Add(cookie);
  36. list.Add(reader.ReadToEnd());
  37. }
  38. }
  39. }
  40. catch (WebException wex)
  41. {
  42. WebResponse wr = wex.Response;
  43. using (Stream st = wr.GetResponseStream())
  44. {
  45. using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
  46. {
  47. list.Add(sr.ReadToEnd());
  48. }
  49. }
  50. }
  51. catch (Exception ex)
  52. {
  53. list.Add("发生异常/n/r"+ex.Message);
  54. }
  55. return list;
  56. }

4。就是第三步请求的链接地址换一个就行了

好了

以上核心代码已经贴出了

具体实现需要靠你们按照你们自己的逻辑

还有一些header能不写就不写,因为我2天前一直在获取返回response这地方报500错误。

找了N多代码,看了N多资料都不可以。最后将一些header注释掉就可以了,真郁闷。

最新文章

  1. 20个设计精致的用户界面 PSD 源文件免费下载
  2. 关于Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)的问题
  3. CSS实现图片快速等比例缩放,效果佳
  4. [转] gc tips(2)
  5. CString 转 char*; wstring 转 string
  6. Nginx的session一致性问题
  7. 【HDOJ】1857 Word Puzzle
  8. 24、Javascript BOM
  9. C、C++中“*”操作符和“后++”操作符的优先级
  10. Visual Studio 2013 Preview - ASP.NET, MVC 5, Web API 2新功能搶先看
  11. IM比较SipDroid/IMSDroid/CSipsimple/Linphone/Webrtc
  12. [bzoj2836] 魔法树
  13. WPF基础篇之控件模板(ControlTemplate)
  14. Winform Focus()函数不起作用 解决办法
  15. python excle读
  16. PHP+XML写注册登录
  17. [转]如何查看oracle用户具有的权限和角色
  18. 解决easyui combobox赋值boolean类型的值时,经常出现的内容显示的value而不是text的bug
  19. Django--cookie 和 session
  20. 针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1)

热门文章

  1. Python3 操作Excel--openpyxl
  2. udp接收
  3. 项目中处理android 6.0权限管理问题
  4. C#删除只读文件或文件夹(解决File.Delete无法删除文件)
  5. 获取checked的值
  6. tomcat服务配置及搭建
  7. Abstraction elimination
  8. [SOJ] shortest path in unweighted graph
  9. sharepoint代码添加WebPart
  10. 给go添加各种package