C#获取网页源代码的方法
程序员文章站
2022-06-21 08:50:35
本文实例讲述了c#获取网页源代码的方法。分享给大家供大家参考。具体如下:
public string getpagehtml(string url)
{
t...
本文实例讲述了c#获取网页源代码的方法。分享给大家供大家参考。具体如下:
public string getpagehtml(string url) { try { httpwebrequest wr = webrequest.create(url) as httpwebrequest; wr.method = "get"; wr.accept = "*/*"; wr.headers.add("accept-language: zh-cn"); wr.headers.add("ua-cpu: x86"); wr.headers.add("accept-encoding: gzip, deflate"); wr.useragent = "mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; embedded web browser from: http://bsalsa.com/; infopath.2; .net clr 2.0.50727; .net clr 3.0.04506.648; .net clr 3.5.21022)"; wr.keepalive = true; wr.servicepoint.expect100continue = false; wr.allowautoredirect = false; httpwebresponse wre = wr.getresponse() as httpwebresponse; streamreader sreader = new streamreader(wre.getresponsestream(), encoding.getencoding("gbk")); string shtml = sreader.readtoend(); wre.close(); return shtml; } catch { return ""; } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: C#实现子窗体与父窗体通信方法实例总结
下一篇: Unity相机移动之屏幕边缘检测