zoukankan      html  css  js  c++  java
  • html源码获取方法

    GetURLHtml.aspx   
        
      
    <%@   Page   language="c#"   Codebehind="GetURLHtml.aspx.cs"   Src="GetURLHtml.aspx.cs"   AutoEventWireup="false"   Inherits="Lion.Web.Forum.WebForm1"   %>   
      
    <HTML>   
        
    <HEAD>   
          
    <title>Lion互动网络=>利用WebClient和WebRequest类获得网页源代码</title>   
          
    <META   http-equiv="Content-Type"   content="text/html;   charset=gb2312">   
        
    </HEAD>   
        
    <body>   
          
    <form   runat="server">   
            
    <asp:TextBox   id="UrlText"   runat="server"   Width="50%">http://www.lionsky.net/mywebsite/index.aspx</asp:TextBox>   
            <asp:Button   id="Button1"   runat="server"   Text="用WebClient得到"></asp:Button>&nbsp;   
            
    <asp:Button   id="Button2"   runat="server"   Text="用WebRequest得到"></asp:Button><BR>   
            
    <asp:TextBox   id="ContentHtml"   runat="server"   Width="100%"   Height="360px"   TextMode="MultiLine"></asp:TextBox>   
          
    </form>   
        
    </body>   
      
    </HTML>   
        
      GetURLHtml.aspx.Cs   
        
      
    using   System;   
      
    using   System.Collections;   
      
    using   System.ComponentModel;   
      
    using   System.Data;   
      
    using   System.Drawing;   
      
    using   System.Web;   
      
    using   System.Web.SessionState;   
      
    using   System.Web.UI;   
      
    using   System.Web.UI.WebControls;   
      
    using   System.Web.UI.HtmlControls;   
        
      
    namespace   Lion.Web.Forum   
      {   
        
    ///   <summary>   
        
    ///   WebForm1   的摘要说明。   
        
    ///   </summary>   
        public   class   WebForm1   :   System.Web.UI.Page   
        {   
          
    protected   System.Web.UI.WebControls.TextBox   TextBox1;   
          
    protected   System.Web.UI.WebControls.Button   Button1;   
          
    protected   System.Web.UI.WebControls.Button   Button2;   
          
    protected   System.Web.UI.WebControls.TextBox   UrlText;   
          
    protected   System.Web.UI.WebControls.TextBox   ContentHtml;   
          
    protected   System.Web.UI.WebControls.TextBox   TextBox2;   
          
          
    #region   Web   窗体设计器生成的代码   
          
    override   protected   void   OnInit(EventArgs   e)   
          {   
            
    //   
            
    //   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。   
            
    //   
            InitializeComponent();   
            
    base.OnInit(e);   
          }   
            
          
    ///   <summary>   
          
    ///   设计器支持所需的方法   -   不要使用代码编辑器修改   
          
    ///   此方法的内容。   
          
    ///   </summary>   
          private   void   InitializeComponent()   
          {           
            
    this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);   
            
    this.Button2.Click   +=   new   System.EventHandler(this.Button2_Click);   
        
          }   
          
    #endregion   
        
          
    private   void   Button1_Click(object   sender,   System.EventArgs   e)   
          {   
            
    string   PageUrl   =   UrlText.Text;   
            System.Net.WebClient   wc   
    =   new   System.Net.WebClient();   
            wc.Credentials   
    =   System.Net.CredentialCache.DefaultCredentials;   
        
            
    ///方法一:   
            Byte[]   pageData   =   wc.DownloadData(PageUrl);   
            ContentHtml.Text   
    =   System.Text.Encoding.Default.GetString(pageData);   
        
            
    ///   方法二:   
            
    ///   ***************代码开始**********   
            
    ///   Stream   resStream   =   wc.OpenRead(PageUrl);   
            
    ///   StreamReader   sr   =   new   StreamReader(resStream,System.Text.Encoding.Default);   
            
    ///   ContentHtml.Text   =   sr.ReadToEnd();   
            
    ///   resStream.Close();   
            
    ///   **************代码结束********   
            
    ///     
            wc.Dispose();       
        
          }   
        
          
    private   void   Button2_Click(object   sender,   System.EventArgs   e)   
          {   
            
    string   PageUrl   =   UrlText.Text;   
            System.Net.WebRequest     request   
    =   System.Net.WebRequest.Create(PageUrl);   
            System.Net.WebResponse   response   
    =   request.GetResponse();   
            System.IO.Stream   resStream   
    =   response.GetResponseStream();     
            System.IO.StreamReader   sr   
    =   new   System.IO.StreamReader(resStream,   System.Text.Encoding.Default);   
            ContentHtml.Text   
    =   sr.ReadToEnd();   
            resStream.Close();     
            sr.Close();   
        
          }   
        }   
      } 
  • 相关阅读:
    (转)Caffe搭建:常见问题解决办法和ubuntu使用中遇到问题(持续更新)
    Ubuntu14.04配置python接口,测试的小问题
    ubuntu简单的小命令
    ubuntu14.04&matlab2015b 测试caffe的Matlab接口
    0427 进制转换
    windows下安装nginx
    事务管理配置与@Transactional注解使用
    spring-web涉及jar包说明
    20145207 《信息安全系统设计基础》第1周学习总结
    《信息安全程序设计基础》第零周学习总结
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1540606.html
Copyright © 2011-2022 走看看