zoukankan      html  css  js  c++  java
  • c# 用System.Net 读取网页源代码 1

       查看了好多抓取网页的代码,都没有如下这样书写的,故先记与此。

    首先引用System.Web.dll;

    using System.Net;
    using System.IO; 

      private void Page_Load(object sender, System.EventArgs e)
      {
       // 在此处放置用户代码以初始化页面
       string url="http:www.sina.com";
       System.IO.Stream stream=null;
       WebClient  client=new WebClient();
       stream=client.OpenRead(url);
       
       StreamReader readerOfStream = new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));
       string aa= readerOfStream.ReadToEnd();
       Response.Write(aa);
       // Close the stream.
       stream.Close();

      }

     private void InitializeComponent()
      {   
       this.Load += new System.EventHandler(this.Page_Load);

      }

    Microsoft Visual Studio .net 2003 下运行成功。

    接上文,以下是普遍采用的 c# 用System.Net 读取网页源代码 的书写方式。

       System.Net.HttpWebRequest req;
       System.Net.HttpWebResponse res;
       string url="www.paopaokdc.org.cn";
       req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
       res = (System.Net.HttpWebResponse)req.GetResponse();
       System.IO.StreamReader strm = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
       string aa=strm.ReadToEnd();
       Response.Write(aa);

      方法2

      在WindowsApplication 用WebBrowser控件实现 读取网页。  
      在工具箱里右击,选添加/删除选项,在弹出的对话框里选Com组件选项卡,找到   
      Microsoft  Web 浏览器 选定 后确定, 在工具箱里选择WebBrowser控件,拖放到窗体上,然后写代码:   

    private void Form1_Load(object sender, System.EventArgs e)
      {
        string   str="";  
       System.Object   nullObject=0;  
       System.Object   nullObjStr=str;  
       this.axWebBrowser1.Navigate("www.paopaokdc.org.cn",ref   nullObject,ref    
       
        nullObjStr,ref   nullObjStr,ref   nullObjStr);  
      }


     

  • 相关阅读:
    TAM实施范例
    xmanager连接到RHEL6.
    TAM安装过程中遇到的问题
    db29.1FP2升级FP12
    WAS常见问题及解答
    在 Lotus Quickr for Domino 环境中使用 Tivoli Access Manager WebSEAL 作为反向代理服务器
    TAM包含的内容全面的指南自IBM
    Setting up the Web Admin Tool in LDAP 6.x to communicate via SSL
    oracle字符集。
    redhat中设置环境变量PATH的方法和只显示目录的Tree
  • 原文地址:https://www.cnblogs.com/flashicp/p/644557.html
Copyright © 2011-2022 走看看