C#代碼處理網頁關於登錄的code


作者:血飲狂龍
鏈接:https://www.zhihu.com/question/49452639/answer/117294801
來源:知乎
著作權歸作者所有,轉載請聯系作者獲得授權。

private string getHtml(string url) { HttpItem item = new HttpItem() { URL = url, Encoding = null, Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", KeepAlive = false, UserAgent = userAgent, Expect100Continue = true, Header = new WebHeaderCollection() { {"Accept-Encoding","gzip, deflate"}, {"Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"} } }; HttpResult result = http.GetHtml(item); return result.Html; } 
以上代碼是再入一個普通網頁的代碼,采用get方法。下面再貼一段采用post方法的代碼:
 private string postHtml(string url, string postData, string referer) { HttpItem item = new HttpItem() { URL = url, Encoding = null, Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", KeepAlive = true, ContentType = "application/x-www-form-urlencoded", Referer = referer, UserAgent = userAgent, Expect100Continue = false, Method = "POST", Postdata = postData, Header = new WebHeaderCollection() { {"Accept-Encoding","gzip, deflate"}, {"Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"} } }; HttpResult result = http.GetHtml(item); return result.Html; } 
上面這段代碼一般用於登錄之類的提交請求的動作,比如登錄。
下面提供一點兒干貨:
想學習爬蟲的童鞋,你們一定要看看這個網站: [C#HttpHelper]官方產品發布與源碼下載 蘇菲論壇,這個人把登錄網頁封裝起來用,很方便喲。
你需要用到的工具有:httpwatch或者fiddler抓取網頁數據。
win10的親們,你們需要被特別照顧一下,因為那蛋疼的ie並不兼容httpwatch。firefox或者chrome目前對插件也管控特別嚴,因此你們需要用到以下組合:
firefox 35.0b8+Httpwatch9.4.17


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM