zoukankan      html  css  js  c++  java
  • asp.net C# 获取网页源代码的几种方式

    1 方法
                   System.Net.WebClient aWebClient = new System.Net.WebClient();
                   aWebClient.Encoding = System.Text.Encoding.Default;
                   Byte[] pageData = aWebClient.DownloadData(url);
               
                   string nhtml = Encoding.GetEncoding("utf-8").GetString(pageData);

    2方法

                    System.Net.WebClient aWebClient = new System.Net.WebClient();
                    aWebClient.Encoding = System.Text.Encoding.Default;
                    string nhtml = aWebClient.DownloadString(goodstidurl);

    3方法
                   WebBrowser webbrowser = new WebBrowser();
                    StreamReader sr = new StreamReader(this.webBTaobao.DocumentStream, Encoding.Default);
                    html = sr.ReadToEnd();
                    html = html.Replace("
    ", "");
                    html = html.Replace("
    ", "");
                    html = html.Replace("  ", "");
                    html = html.Replace("(", "");
                    html = html.Replace(")", "");
                    string nurl = Regex.Match(html, "(?<=data-url=").*?(?=")").Value;
                    //新建一个WebBrowser
                    WebBrowser webAddress = new WebBrowser();
                    webAddress.Navigate(nurl);
                    //等待载入完毕
                    while (webAddress.ReadyState < WebBrowserReadyState.Complete) Application.DoEvents();
                    StreamReader sraddress = new StreamReader(webAddress.DocumentStream, Encoding.Default);
                    jsonaddress = sraddress.ReadToEnd();

    4方法

                WebRequest hwr = WebRequest.Create(@"http://item.taobao.com/item.htm?

    id=" + row["urlId"].ToString());//向指定Url发出请求 HttpWebResponse hwp = hwr.GetResponse() as HttpWebResponse;//将hwr对HTTP的请求 string text; StreamReader sr; string code = hwp.ContentType;//请求响应得到的内容类型 //得到编码了 code = code.Split('=')[1]; Stream rep = hwp.GetResponseStream();//将请求得到的内容以流的形式读出 sr = new StreamReader(rep, Encoding.GetEncoding(code));//用指定的字符编码为指定的流初始化



  • 相关阅读:
    js的数组问题
    2014的希冀
    js事件在不同浏览器之间的差异
    jackson 实体转json 为NULL或者为空不参加序列化
    spring的嵌套事务
    Mysql的事务及行级锁
    Nginx简介及配置文件详解
    springmvc常用注解标签详解
    springmvc相关配置和用法
    Java并发编程(一):进程和线程之由来
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7069268.html
Copyright © 2011-2022 走看看