国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c# 使用 HttpWebRequest模擬登陸(附帶驗證碼)

2019-11-17 03:12:53
字體:
來源:轉載
供稿:網友
c# 使用 HttpWebRequest模擬登陸(附帶驗證碼

在C#中,可以使用HttpWebRequest進行相關的模擬登陸,登陸后進行相關的操作,比如抓取數據,頁面分析,制作相關登陸助手等等。

先說下流程

1.使用httpwebrequest先進入你要登錄的網站,獲取cookie

2.使用第一步獲取的cookie到驗證碼的網頁將驗證碼下載下來。

3.使用Post數據 發送至網站。如果有cookie則繼續保存。

4.使用第三步的cookie登陸相關網頁操作。

獲取相關數據可以使用抓包工具進行抓取,如httpwatch。(網上下載的好多都有病毒,下載的時候注意點)

1。

[c-sharp]view plaincopy
  1. ///<summary>
  2. ///通過get方式請求頁面,傳遞一個實例化的cookieContainer
  3. ///</summary>
  4. ///<paramname="postUrl"></param>
  5. ///<paramname="cookie"></param>
  6. ///<returns></returns>
  7. publicstaticArrayListGetHtmlData(stringpostUrl,CookieContainercookie)
  8. {
  9. HttpWebRequestrequest;
  10. HttpWebResponseresponse;
  11. ArrayListlist=newArrayList();
  12. request=WebRequest.Create(postUrl)asHttpWebRequest;
  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(StreamReaderreader=newStreamReader(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(WebExceptionex)
  34. {
  35. list.Clear();
  36. list.Add("發生異常/n/r");
  37. WebResponsewr=ex.Response;
  38. using(Streamst=wr.GetResponseStream())
  39. {
  40. using(StreamReadersr=newStreamReader(st,System.Text.Encoding.Default))
  41. {
  42. list.Add(sr.ReadToEnd());
  43. }
  44. }
  45. }
  46. catch(Exceptionex)
  47. {
  48. list.Clear();
  49. list.Add("5");
  50. list.Add("發生異常:"+ex.Message);
  51. }
  52. returnlist;
  53. }

2.下載驗證碼,保存在本地。

[c-sharp]view plaincopy
  1. ///<summary>
  2. ///下載驗證碼圖片并保存到本地
  3. ///</summary>
  4. ///<paramname="Url">驗證碼URL</param>
  5. ///<paramname="cookCon">Cookies值</param>
  6. ///<paramname="savePath">保存位置/文件名</param>
  7. publicstaticboolDowloadCheckImg(stringUrl,CookieContainercookCon,stringsavePath)
  8. {
  9. boolbol=true;
  10. HttpWebRequestwebRequest=(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;MSIE6.0;WindowsNT5.1;SV1;Maxthon;.NETCLR1.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","gz
  21. webRequest.KeepAlive=true;
  22. webRequest.CookieContainer=cookCon;
  23. try
  24. {
  25. //獲取服務器返回的資源
  26. using(HttpWebResponsewebResponse=(HttpWebResponse)webRequest.GetResponse())
  27. {
  28. using(Streamsream=webResponse.GetResponseStream())
  29. {
  30. List<byte>list=newList<byte>();
  31. while(true)
  32. {
  33. intdata=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(WebExceptionex)
  43. {
  44. bol=false;
  45. }
  46. catch(Exceptionex)
  47. {
  48. bol=false;
  49. }
  50. returnbol;
  51. }

3。發送post數據

[c-sharp]view plaincopy
  1. ///<summary>
  2. ///發送相關數據至頁面
  3. ///進行登錄操作
  4. ///并保存cookie
  5. ///</summary>
  6. ///<paramname="postData"></param>
  7. ///<paramname="postUrl"></param>
  8. ///<paramname="cookie"></param>
  9. ///<returns></returns>
  10. publicstaticArrayListPostData(stringpostData,stringpostUrl,CookieContainercookie)
  11. {
  12. ArrayListlist=newArrayList();
  13. HttpWebRequestrequest;
  14. HttpWebResponseresponse;
  15. ASCIIEncodingencoding=newASCIIEncoding();
  16. request=WebRequest.Create(postUrl)asHttpWebRequest;
  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(Streamstream=request.GetRequestStream())
  23. {
  24. stream.Write(b,0,b.Length);
  25. }
  26. try
  27. {
  28. //獲取服務器返回的資源
  29. using(response=request.GetResponse()asHttpWebResponse)
  30. {
  31. using(StreamReaderreader=newStreamReader(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(WebExceptionwex)
  41. {
  42. WebResponsewr=wex.Response;
  43. using(Streamst=wr.GetResponseStream())
  44. {
  45. using(StreamReadersr=newStreamReader(st,System.Text.Encoding.Default))
  46. {
  47. list.Add(sr.ReadToEnd());
  48. }
  49. }
  50. }
  51. catch(Exceptionex)
  52. {
  53. list.Add("發生異常/n/r"+ex.Message);
  54. }
  55. returnlist;
  56. }

4。就是第三步請求的鏈接地址換一個就行了

好了

以上核心代碼已經貼出了

具體實現需要靠你們按照你們自己的邏輯

還有一些header能不寫就不寫,因為我2天前一直在獲取返回response這地方報500錯誤。

找了N多代碼,看了N多資料都不可以。最后將一些header注釋掉就可以了,真郁悶。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定州市| 嘉黎县| 华容县| 临泽县| 达孜县| 灵寿县| 宣城市| 大邑县| 信丰县| 新乐市| 承德县| 安庆市| 芷江| 东乌| 金川县| 宣威市| 清丰县| 靖州| 苏尼特右旗| 宣武区| 德州市| 东乡| 海安县| 南丰县| 佛冈县| 绿春县| 昭觉县| 莱芜市| 泰州市| 玛曲县| 黎川县| 新巴尔虎左旗| 斗六市| 澄城县| 上林县| 怀安县| 朝阳市| 惠东县| 庐江县| 商都县| 青铜峡市|