HTML:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head id="Head1" runat="server">
  7. <title>得到网页源代码</title>
  8. </head>
  9. <body MS_POSITIONING="GridLayout">
  10. <form id="aspNetBuffer" method="post" runat="server">
  11. <div align="center" style="FONT-WEIGHT: bold">得到任意网页源代码</div>
  12. <asp:TextBox id="UrlText" runat="server" Width="400px">http://dotnet.aspx.cc/content.aspx
  13. </asp:TextBox>
  14. <asp:Button id="WebClientButton" Runat="server" Text="用WebClient得到" OnClick="WebClientButton_Click"></asp:Button>
  15. <asp:Button id="WebRequestButton" runat="server" Text="用WebRequest得到" OnClick="WebRequestButton_Click"></asp:Button>
  16.  
  17. <asp:TextBox id="ContentHtml" runat="server" Width="100%" Height="360px" TextMode="MultiLine">
  18. </asp:TextBox>
  19. </form>
  20. </body>
  21. </html>

ASPX:

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. using System.Net;
  13. using System.Text;
  14. public partial class _Default : System.Web.UI.Page
  15. {
  16. private string PageUrl = "";
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19.  
  20. }
  21. protected void WebClientButton_Click(object sender, EventArgs e)
  22. {
  23. PageUrl = UrlText.Text;
  24. WebClient wc = new WebClient();
  25. wc.Credentials = CredentialCache.DefaultCredentials;
  26. ///方法一:
  27. Byte[] pageData = wc.DownloadData(PageUrl);
  28. ContentHtml.Text = Encoding.Default.GetString(pageData);
  29.  
  30. /// 方法二:
  31. /// ***************代码开始**********
  32. /// Stream resStream = wc.OpenRead(PageUrl);
  33. /// StreamReader sr = new StreamReader(resStream,System.Text.Encoding.Default);
  34. /// ContentHtml.Text = sr.ReadToEnd();
  35. /// resStream.Close();
  36. /// **************代码结束********
  37. ///
  38. wc.Dispose();
  39. }
  40. protected void WebRequestButton_Click(object sender, EventArgs e)
  41. {
  42. PageUrl = UrlText.Text;
  43. WebRequest request = WebRequest.Create(PageUrl);
  44. WebResponse response = request.GetResponse();
  45. Stream resStream = response.GetResponseStream();
  46. StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
  47. ContentHtml.Text = sr.ReadToEnd();
  48. resStream.Close();
  49. sr.Close();
  50.  
  51. }
  52. }

WebClient和WebRequest获取html代码的更多相关文章

  1. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

  2. c#利用WebClient和WebRequest获取网页源代码

    C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现.   WebClient类获取网页源代码   WebClient类   WebClient ...

  3. 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)

    一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...

  4. WebRequest 获取网页乱码

    问题:在用WebRequest获取网页源码时得到的源码是乱码. 原因:1,编码不对 解决办法:设置对应编码 WebRequest request = WebRequest.Create(Url);We ...

  5. [PHP] debug_backtrace()可以获取到代码的调用路径追踪

    查看代码的时候,看到有使用这个函数,测试一下 1.debug_backtrace()可以获取到代码的调用追踪,以数组形式返回 2.debug_print_backtrace() — 打印一条回溯,直接 ...

  6. Eclipse已经安装了SVN插件,但是在获取SVN代码时,一直处于progress....

    Eclipse已经安装了SVN插件,但是在获取SVN代码时,一直处于progress.... 后来升级把SVN插件升级到了1.10x,在获取就看轻轻松松搞定了 由此得出: 在安装EclipseSVSN ...

  7. Csharp:WebClient and WebRequest use http download file

    //Csharp:WebClient and WebRequest use http download file //20140318 塗聚文收錄 string filePath = "20 ...

  8. discuz开发实现自动获取后台入口代码

    一般discuz后台入口默认是admin.php,不过部分用户为了安全可能会修改后台入口文件名称,可以用代码 '.ADMINSCRIPT.'?frame=no&action=tools& ...

  9. C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值

    //C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值,表达式如下: Regex regImg = new Regex(@"(?is)<a[^>]*?href ...

随机推荐

  1. You Don't Know JS: Async & Performance(第2章,Callbacks)

    Chapter 2: Callbacks. Callbacks are by far the most common way that asynchrony in JS programs is exp ...

  2. 内核开启VF小结

    2017-8-29 16:33:40 内核开启VF小结: 1. eth2上创建4个VFecho 4 > /sys/class/net/eth2/device/sriov_numvfs2. 关闭e ...

  3. VS Code行内样式提示插件

    打开vscode,在软件界面左下角找到“齿轮”标志并点击,在弹出的菜单中选择“设置”,把下面的代码添加到设置里. { "workbench.colorTheme": "C ...

  4. upsource初探

    在JetBrains 的官网上,看到codereview的工具 upsource ,https://www.jetbrains.com/upsource/  官方的英文文档 来看下博客园上有博主简单的 ...

  5. ssh -vT git@github.com get “ No such file or directory” 错误

    在"Are you sure you want to continue connnecting"的时候回复y,不要打空格跳过 参考:http://stackoverflow.com ...

  6. 部署java项目到服务器

    1.首先判断服务器是什么系统 linux,windows 2.如果是linux使用SSH进行链接 3.如果是windows使用远程桌面进行链接 1.windows+R->mstsc进行远程桌面的 ...

  7. Analytic Functions in Oracle

    Contents Overview and IntroductionHow Analytic Functions WorkThe SyntaxExamplesCalculate a running T ...

  8. IDA 逆向工程 反汇编使用

    IDA pro 7.0版本 from:freebuf 用到的工具有IDA pro 7.0  ,被反汇编的是百度云(BaiduNetdisk_5.6.1.2.exe). 首先,IDA pro的长相如下: ...

  9. telnet的装配及xinetd的讨论

    telnet由于是不安全的明文传输所以基本被ssh取代了,尤其是在Linux上:不过还是可能会用到,且启停方式还有些不同所以还是有必要说明一下. rpm -qa | grep telnet #查看是否 ...

  10. sql中,如何获取一个数的整数部分和余数部分

    我们测试一下,我要得到的结果是多少周(整数),多少天(余数) 1.获取指定日期到当前日期之间的天数 首先用DATEDIFF() 函数获取指定日期到当前日期的天数 --获取指定日期到当前日期的天数 se ...