网上已经有类似的代码,但是效率很低,以下代码非常快速完成。本人在支付宝免签中使用以下方式。
//获取浏览器的cookies string Webcookies = webBrowser.Document.Cookie; //通过HTTP请求加载测试 HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = this.contacturl, Method = "get", Encoding = Encoding.UTF8, Cookie = Webcookies, Accept= "application/json, text/javascript, */*; q=0.01", ContentType = "text/plain", UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", }; item.Header.Add("Accept-Language", "zh-CN,zh;q=0.8"); item.Header.Add("X-Requested-With", "XMLHttpRequest"); item.Header.Add("Accept-Encoding", "gzip, deflate, sdch, br"); //得到HTML代码 HttpResult result = http.GetHtml(item); string html = result.Html;
HttpHelper类由苏飞大神提供。需要可直接联系我索取。