C#完成的三種模仿主動登錄和提交POST信息的辦法。本站提示廣大學習愛好者:(C#完成的三種模仿主動登錄和提交POST信息的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的三種模仿主動登錄和提交POST信息的辦法正文
本文實例講述了C#完成的三種模仿主動登錄和提交POST信息的辦法。分享給年夜家供年夜家參考,詳細以下:
網頁主動登錄(提交Post內容)的用處許多,如驗證身份、法式進級、收集投票等,以下是用C#完成的辦法。
網頁主動登錄和提交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("xxx.htm") > 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://xxxx.com.cn/tp.asp?Id=129")); //翻開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://xxxxxxy.com.cn/tp.asp?Id=129"), "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://www.xxxx.com/chk.asp")); 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異樣供給了異步操作,有興致的同伙本身查MSDN,完成起來也不難。
客戶端法式模仿post提交的用途許多,常常用於分歧平台間的接口交互,
樓主總結的很好,不外少了一中辦法:
WebRequest request = WebRequest.Create(Url); request.Method = "POST"; request.Timeout = 100000; request.GetRequestStream().Close(); WebResponse response = request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8); webInfo = sr.ReadToEnd(); sr.Close();
也挺便利
願望本文所述對年夜家C#法式設計有所贊助。