Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP.NET > WebClient和WebRequest获取html代码
【标  题】:WebClient和WebRequest获取html代码
【关键字】:WebClient,WebRequest,html
【来  源】:http://blog.csdn.net/zhanghefu/archive/2007/01/28/1496404.aspx

WebClient和WebRequest获取html代码

Your Ad Here

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>得到网页源代码</title>
</head>
  <body MS_POSITIONING="GridLayout">
    <form id="aspNetBuffer" method="post" runat="server">
      <div align="center" style="FONT-WEIGHT: bold">得到任意网页源代码</div>
      <asp:TextBox id="UrlText" runat="server" Width="400px">http://dotnet.aspx.cc/content.aspx
       </asp:TextBox>
      <asp:Button id="WebClientButton" Runat="server" Text="用WebClient得到" OnClick="WebClientButton_Click"></asp:Button>
      <asp:Button id="WebRequestButton" runat="server" Text="用WebRequest得到" OnClick="WebRequestButton_Click"></asp:Button>
     
      <asp:TextBox id="ContentHtml" runat="server" Width="100%" Height="360px" TextMode="MultiLine">
       </asp:TextBox>
    </form>
  </body>
</html>

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;
using System.IO;
using System.Net;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
    private string PageUrl = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void WebClientButton_Click(object sender, EventArgs e)
    {
        PageUrl = UrlText.Text;
        WebClient wc = new WebClient();
        wc.Credentials = CredentialCache.DefaultCredentials;
        ///方法一:
        Byte[] pageData = wc.DownloadData(PageUrl);
        ContentHtml.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();
    }
    protected void WebRequestButton_Click(object sender, EventArgs e)
    {
        PageUrl = UrlText.Text;
        WebRequest request = WebRequest.Create(PageUrl);
        WebResponse response = request.GetResponse();
        Stream resStream = response.GetResponseStream();
        StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
        ContentHtml.Text = sr.ReadToEnd();
        resStream.Close();
        sr.Close();

    }
}
 

通用ASP分页类:【上一篇】
自定义控件设计:通过gridview传递参数:【下一篇】
【相关文章】
  • HTML代码之Marquee详解
  • SHTML教程
  • XHTML学习摘要一
  • 写了一个还原html代码的正则
  • html中的特殊符号
  • 一个功能强大使用超简单的纯客户端在线HTML编辑器有源码
  • C++2HTML
  • HTML&CSS开发规范【摘自领域'S Blog】
  • XHTML: Differences between Strict
  • HTML 和 PHP 、MySQL 的交互
  • 【随机文章】
  • 为Visual FoxPro开发者建立移动解决方案
  • tomcat5.5 配置oracle连接池
  • Java 理论与实践:让 J2EE 脱离容器
  • Frontpage2000值得学习的10个操作技巧(下)
  • 在linux下使用 Ramdisk(转贴)
  • 文档-编程文档
  • MSN7来了
  • windows下架设subversion服务器
  • 6类布线系统常见问题解答(二)
  • 《Effective C#》Item 15:利用using和try-finally来释放资源
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.