C#網頁自動登錄和提交POST信息的多種方法 新人學習中

網頁自動登錄和提交POST信息的核心就是分析網頁的源代碼(HTML),在C#中,可以用來提取網頁HTML的組件比較多,常用的用WebBrowser、WebClient、HttpWebRequest這三個。

以下就分別用這三種方法來實現:
1、WebBrowser是個"迷你"瀏覽器,其特點是Post時不用關心Cookie、內置JS等問題
WebBrowser是VS2005新提供的組件(其實就是封裝了IE接口),實現POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 來實現,代碼如下:

?????????? HtmlElement ClickBtn =null;
?????????? if (e.Url.ToString().ToLower().IndexOf("http://sandou.cnblogs.com/") > 0)?? //登陸頁面
??????????? {
??????????????? HtmlDocument doc = webBrowser1.Document;
??????????????? for (int i = 0; i < doc.All.Count ; i++)
??????????????? {
??????????????????? if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
??????????????????? {
??????????????????????? switch (doc.All[i].Name)
??????????????????????? {
??????????????????????????? case "userCtl":
??????????????????????????????? doc.All[i].InnerText = "user01";
??????????????????????????????? break;
??????????????????????????? case "passCt1":
??????????????????????????????? doc.All[i].InnerText = "mypass";
??????????????????????????????? break;
??????????????????????????? case "B1":
??????????????????????????????? ClickBtn = doc.All[i]; //提交按鈕
??????????????????????????????? break;
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? ClickBtn.InvokeMember("Click");?? //執行按扭操作
??????????? }

2、WebClient封裝了HTTP的一些類,操作簡單,相較于webBrowser,特點是可以自設代理,缺點是對COOKIE的控制
WebClient的運行全在后臺,并且提供了異步操作的能力,這樣很方便并發多個任務,然后等待結果的返回,再逐個處理。多任務異步調用的代碼如下:

?private void StartLoop(int ProxyNum)
??????? {
?????????? WebClient []? wcArray = new WebClient[ProxyNum];? //初始化
???????????? for (int idArray = 0; idArray< ProxyNum;idArray++)
??????????? {
???????????????? wcArray[idArray] = new WebClient();
??????????????? wcArray[idArray].OpenReadCompleted += new OpenReadCompletedEventHandler(Pic_OpenReadCompleted2);
??????????????? wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEventHandler(Pic_UploadDataCompleted2);
??????????????? try
??????????????? {
???????????????????
??????????????????? wcArray[idArray].Proxy = new WebProxy(proxy[1], port);
??????????????????? wcArray[idArray].OpenReadAsync(new Uri("http://sandou.cnblogs.com/")); //打開WEB;
??????????????????? proxy = null;
??????????????? }
??????????????? catch
??????????????? {
??????????????? }
??????????? }
??????? }

??????? private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
??????? {
??????????????? if (e.Error == null)
??????????????? {
??????????????????????????? string textData = new StreamReader(e.Result, Encoding.Default).ReadToEnd();? //取返回信息
???????????????????????????? ..
????????????????????????????? String cookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
???????????????????????????? ((WebClient)sender).Headers.Add("Content-Type", "application/x-www-form-urlencoded");
??????????????????????????? ((WebClient)sender).Headers.Add("Accept-Language", "zh-cn");
??????????????????????????? ((WebClient)sender).Headers.Add("Cookie", cookie);

??????????????????????????? string postData = ""
??????????????????????????? byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 轉化成二進制數組
?????????????????????????? ((WebClient)sender).UploadDataAsync(new Uri("http://sandou.cnblogs.com/"), "POST", byteArray);
??????????????? }
???????? }

??????? private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEventArgs e)
??????? {
???????????????? if (e.Error == null)
??????????????? {
??????????????????? string returnMessage = Encoding.Default.GetString(e.Result);
???????????????????
??????????????? }
?????? }

?


3、HttpWebRequest較為低層,能實現的功能較多,Cookie操作也很簡單:

?
?????? private bool? PostWebRequest()???????
??????? {
?????????????????? CookieContainer cc = new CookieContainer();
??????????????????? string pos tData = "user=" + strUser + "&pass=" + strPsd;
??????????????????? byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 轉化

??????????????????? HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(http://sandou.cnblogs.com/));
??????????????????? webRequest2.CookieContainer = cc;
??????????????????? webRequest2.Method = "POST";
??????????????????? webRequest2.ContentType = "application/x-www-form-urlencoded";
??????????????????? webRequest2.ContentLength = byteArray.Length;
??????????????????? Stream newStream = webRequest2.GetRequestStream();
??????????????????? // Send the data.
??????????????????? newStream.Write(byteArray, 0, byteArray.Length);??? //寫入參數
??????????????????? newStream.Close();

??????????????????? HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
??????????????????? StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
??????????????????? string text2 =? sr2.ReadToEnd();
?????????????????
??????? }????

HttpWebRequest 實現, 這個是從網上COPY 的!我以前用相關的代碼登錄到WWW.ASP.NET上,并且成功post,可惜代碼不知道放什么地方了。

HttpWebRequest自動登錄網站并獲取網站內容(不包含驗證碼的網站)
可以使用 Visual Sniffer(百度搜索) 來捕捉提交的數據信息:
1. 訪問你需要站外提交的頁面,比如 CSDN 登陸頁 http://www.csdn.net/member/UserLogin.aspx
2. 填寫好需要的資料,比如用戶名和密碼,
3. 打開 Visual Sniffer, 點“開始攔截”
4. 在訪問的頁面中提交。
5. 等提交成功之后,在 Visual Sniffer 中“停止攔截”
6. 在 Visual Sniffer 的左側欄的加號中依次點開,右邊是它攔截到的內容:

?
POST http://www.csdn.net/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Content-Length: 355
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879

__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxDU0ROVXNlckxvZ2luOmNiX1NhdmVTdGF0ZTtDU0ROVXNlckxvZ2luOkltYWdlX0xvZ2luOz4%2Btu1q2wmRZoAJTi9L73w1zBleylY%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDNUserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232&from=&CSDNUserLogin%3AImage_Login.x=36&CSDNUserLogin%3AImage_Login.y=6
GET http://www.csdn.net/mycustompage.htm?aspxerrorpath=/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879
以上為攔截內容,其中提交數據的參數部分(程序中的:strArgs)如:
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxDU
0ROVXNlckxvZ2luOmNiX1NhdmVTdGF0ZTtDU0ROVXNlckxvZ2luOkltYWdlX0xvZ2luOz4%2Btu
1q2wmRZoAJTi9L73w1zBleylY%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDN
UserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232


?
??????? protected static string cookieHeader;
??????? private void Page_Load(object sender, System.EventArgs e)
??????? {
??????????? string strReContent = string.Empty;
??????????? //登錄
??????????? strReContent = PostLogin("http://www.mystand.com.cn/login/submit.jsp提交的頁面","提交的參數:userid=hgj0000&password=06045369","引用地址:http://www.mystand.com.cn/");
??????????? //asp.net登錄傳遞的參數需注意???
??????????? //strReContent = PostLogin("http://www.mystand.com.cn/login.aspx","__VIEWSTATE=dDwtNjkzMjUyNDczO3Q8O2w8aTwzPjs%2BO2w8dDxwPHA8bDxUZXh0Oz47bDxcZTs%2BPjs%2BOzs%2BOz4%2BOz6aX2dtqkJTK%2BKbNPsjd7Op%2Fl26Iw%3D%3D&txtUserName=hxf&txtPassword=hxf0000&btnEnter=%E7%99%BB%E5%BD%95","http://www.mystand.com.cn/login.aspx");
??????????? //獲取頁面
??????????? strReContent = GetPage("http://www.mystand.com.cn/company/getdata.jsp?code=","引用地址:http://www.mystand.com.cn/");
??????????? //strReContent = GetPage("http://www.mystand.com.cn/Modules/index.aspx","http://www.mystand.com.cn/login.aspx");
??????????? //可以對獲得的內容進行處理:strReContent
??????? }

??????? /** <summary>
??????? /// 功能描述:模擬登錄頁面,提交登錄數據進行登錄,并記錄Header中的cookie
??????? /// </summary>
??????? /// <param name="strURL">登錄數據提交的頁面地址</param>
??????? /// <param name="strArgs">用戶登錄數據</param>
??????? /// <param name="strReferer">引用地址</param>
??????? /// <returns>可以返回頁面內容或不返回</returns>
??????? public static string PostLogin(string strURL,string strArgs,string strReferer)
??????? {
??????????? string strResult = "";
??????????? HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
??????????? myHttpWebRequest.AllowAutoRedirect = true;
??????????? myHttpWebRequest.KeepAlive = true;
??????????? myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
??????????? myHttpWebRequest.Referer = strReferer;
???????????
??????????? myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
??????????? myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
??????????? myHttpWebRequest.Method = "POST";

??????????? CookieCollection myCookies = null;
??????????? CookieContainer myCookieContainer = new CookieContainer();
??????????? myHttpWebRequest.CookieContainer = myCookieContainer;

??????????? Stream MyRequestStrearm = myHttpWebRequest.GetRequestStream();
??????????? StreamWriter MyStreamWriter = new StreamWriter(MyRequestStrearm,Encoding.ASCII);
??????????? //把數據寫入HttpWebRequest的Request流
??????????? MyStreamWriter.Write(strArgs);
??????????? //關閉打開對象
??????????? MyStreamWriter.Close();
??????????? MyRequestStrearm.Close();

??????????? HttpWebResponse response = null;
??????????? System.IO.StreamReader sr = null;
??????????? response = (HttpWebResponse)myHttpWebRequest.GetResponse();

??????????? cookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL));
??????????? HttpContext.Current.Application.Lock();
??????????? HttpContext.Current.Application["cookieHeader"] = cookieHeader;
??????????? HttpContext.Current.Application.UnLock();
??????????? myCookies = response.Cookies;

??????????? sr = new System.IO.StreamReader(response.GetResponseStream(),Encoding.GetEncoding("gb2312"));??? //??? //utf-8
??????????? strResult = sr.ReadToEnd();
??????????? return strResult;
??????? }

??????? /** <summary>
??????? /// 功能描述:在PostLogin成功登錄后記錄下Headers中的cookie,然后獲取此網站上其他頁面的內容
??????? /// </summary>
??????? /// <param name="strURL">獲取網站的某頁面的地址</param>
??????? /// <param name="strReferer">引用的地址</param>
??????? /// <returns>返回頁面內容</returns>
??????? public static string GetPage(string strURL,string strReferer)
??????? {
??????????? string strResult = "";
??????????? HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
??????????? myHttpWebRequest.ContentType = "text/html";
??????????? myHttpWebRequest.Method = "GET";
??????????? myHttpWebRequest.Referer = strReferer;
??????????? myHttpWebRequest.Headers.Add("cookie:"+ cookieHeader);

??????????? HttpWebResponse response = null;
??????????? System.IO.StreamReader sr = null;
??????????? response = (HttpWebResponse)myHttpWebRequest.GetResponse();
??????????? sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));??? //??? //utf-8
??????????? strResult = sr.ReadToEnd();
??????????? return strResult;
??????? }

技術應用——網頁自動登錄(提交Post內容)的用途很多,如驗證身份、程序升級、網絡投票等,以下是用C#實現的方法.

未解決問題——目前最大問題無法繞過驗證碼——我曾經和同事討論圖片的算法,基本上很難識別,網上也有很多識別驗證碼的例子,但是對于簡單的噪聲還是可以的,可是對于復雜的就一點用都沒有了!到目前為止,我沒有測試成功過!如果你有測試成功過,請帖代碼,我們一起研究研究。

轉載于:https://www.cnblogs.com/gxy217/archive/2012/06/07/2539121.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377462.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377462.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377462.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

四、采集和制作數據集

一、采集數據 安裝labelme&#xff1a;pip install labelme 打開labelme&#xff1a;labelme 將收集好的照片(320320&#xff0c;png格式)存放到一個文件夾中&#xff0c;例如我的是F:\test&#xff0c;再此文件夾下再創建個文件夾label用于存放標簽文件 使用labelme打開數據…

MTFBWU的完整形式是什么?

MTFBWU&#xff1a;愿力量與您同在 (MTFBWU: May The Force Be With You) MTFBWU is an abbreviation of “May The Force Be With You". MTFBWU是“愿力量與你同在”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media n…

VMware14.0 安裝 CentOS7.2

大致流程 對于VMware14.0安裝包用百度網盤下載即可。 鏈接&#xff1a;https://pan.baidu.com/s/1DEGa47EbI1Fup_MTXhv0xg 提取碼&#xff1a;izo6 華為云CentOS7 下載劃線的。其他步驟與大致流程里一樣。 最后輸入root 以及配置的密碼即可&#xff1a;密碼輸入時是沒有任何顯…

基于visual Studio2013解決C語言競賽題之1049抓牌排序

&#xfeff;&#xfeff;&#xfeff;&#xfeff;&#xfeff;&#xfeff;題目解決代碼及點評/* 功能&#xff1a;插入排序。許多玩牌的人是以這樣的方式來對他們手中的牌進行排序的&#xff1a;設手中原有3張牌已排好序&#xff0c;抓1張新牌&#xff0c;若這張新牌的次序在…

學習Lucene筆記一:創建索引

public class HelloLucene {/*** 建立索引* param args*/public void index(){IndexWriter writer null; try {//1.創建Directory,// Directory directory new RAMDirectory();//索引是建立在內存中的Directory directory FSDirectory.open(new File("D:/Lucene/ind…

【C++進階】C++創建文件/屏幕輸出流類(將信息同時輸出到文件和屏幕)

在軟件的調試技術中&#xff0c;很重要的一個技術是將軟件運行過程中的一些信息寫入到“日志文件”中。但是同時還要將信息顯示到屏幕上&#xff0c;以方便程序員實時查看這些信息。 最簡單的一種辦法是這樣的&#xff1a; std::ofstream output("debug.log", ios::…

五、加載數據集

之前寫過加載數據集的一些小筆記&#xff0c;這里詳細內容就不再敘述了 詳細學習可以參考該博文二、PyTorch加載數據 一、分析 因為U-net網絡架構是輸入1通道&#xff0c;大小為(572,572)的灰度圖&#xff0c;圖片大小無所謂&#xff0c;我的思路是將三通道的圖像使用OpenCV進…

CDMA的完整形式是什么?

CDMA&#xff1a;碼分多址 (CDMA: Code Division Multiple Access) CDMA is an abbreviation of Code Division Multiple Access. Code Division Multiple Access is a digital cellular technology and displays a network of multiple accesses. The various radio communica…

BCD碼與十進制的相互轉換

BCD碼是用每四位代替一位十進制數&#xff08;0 到 9 的某一位數&#xff09; 例如&#xff1a;0x25 就代表25 十六進制的每位轉換成二進制代表四個位。 下面是bcd轉char short int long c語言程序 //************************************************************…

DSP關于存儲器讀寫、IO讀寫時序圖的注意點

這里的存儲器圖不涉及插入等待周期。 IO設備的圖可以自行減去插入等待周期&#xff0c;然后觀察。 存儲器讀讀寫 存儲器寫寫讀 I/O設備讀寫操作

折騰430 launchpad

launchpad到手也已經很長時間了&#xff0c;團購了一個g2的&#xff0c;一個鐵電的&#xff0c;現在馬上又要來一個g2的&#xff0c;感覺手上的東西太多了&#xff0c;急需消化一下&#xff0c;首先呢還是先把430搞定吧。 ---------------------------------------------------…

oem模式是什么_OEM的完整形式是什么?

oem模式是什么OEM&#xff1a;原始設備制造商 (OEM: Original Equipment Manufacturer) OEM is an abbreviation of "Original Equipment Manufacturer". Its meaning has changed over time. In former times, it alluded to a corporation that manufactures produ…

媽了個巴卡

配置文件修改&#xff1a; 一、打開PC端微信&#xff0c;打開咩了個咩小程序&#xff0c;點進入第一關&#xff0c;之后再關掉小程序 二、PC端微信設置里面&#xff0c;找到管理文件&#xff0c;打開文件夾 三、Applet下按修改日期找到a9結尾的文件 四、接著進入\usr\gamecac…

java中Iterator的小程序

import java.util.Collection; import java.util.HashSet; import java.util.Iterator;public class TestIterator {public static void main(String[] args){Collection booksnew HashSet();books.add("java講義");books.add("java的Ajax寶典");books.add…

【C++進階】利用重載二元運算符改進平面向量類Vec2D

先前回顧 在【C進階】 遵循TDD原則&#xff0c;實現平面向量類(Vec2D)中我們初步實現了Vec2D內容&#xff0c;現在做出一定的改進&#xff1a; 實現Vec2D的一半二元算數運算符重載 1、 - (兩個Vec2D對象運算以及1個Vec2D對象與一個double數運算) 2、*(點乘和數乘) 同時將之前…

在SQL中使用DEFAULT約束

DEFAULT constraint is used to insert default value into a column on a table and if no any value is stored in any place of a column then default value will be added into it. DEFAULT約束用于將默認值插入到表的列中&#xff0c;如果列的任何位置均未存儲任何值&…

(ios7) 解決代碼布局View, ios7 中 subView 高度增加StatusBar20dp的問題,保證Ios6,ios7代碼一致...

在ios7 布局中&#xff0c;Status Bar 和 ToolBar &#xff0c;NavigateBar 等都包含在ViewControl的主View中。 這樣原來ios6 的View布局 整體向上移動了20dp&#xff0c;下面是保證ios6,ios7代碼一致的解決方案 1 第一步 在項目的Info.plist 文件中 添加一行屬性配置 View co…

簡單的群體測試方案C++代碼(Group testing against Covid-19)

原理參考鏈接 https://www.econstor.eu/handle/10419/221811 http://www.magigen.com/h-nd-348.html 文章原理回顧 文章比較了兩種估計人群中病毒流行率的方法&#xff1a; 1、個體測試&#xff0c;即對12000人的樣本進行病毒測試&#xff0c;并采用標準二項測試得出95%的置…

使用WinDbg和VMware調試NDIS中間層驅動程序 (轉)

使用WinDbg和VMware調試NDIS中間層驅動程序 我這里將一步一步的介紹&#xff0c;是從新手的角度來講的&#xff0c;所以對高手來說&#xff0c;可能有些啰嗦。如果你看完這篇文章還不知道如何設置&#xff0c;那么原因可能有兩個&#xff1a;1. 我沒講好&#xff1b;2. 你需要稍…

c語言字符常量和字符串常量_C語言中的字符常量

c語言字符常量和字符串常量Any character (a single character) that is enclosed within the single quotes (like, A) is called character constants in C programming language. 用單引號引起來的任何字符(單個字符)(例如A ) 在C編程語言中稱為字符常量 。 Character cons…