C#获取网页源码问题 = = 20

http://passport.baidu.com/?business&un=a#0想获取这个的源码,用webclient和webrequest咋都不行呢==写的是win... http://passport.baidu.com/?business&un=a#0 想获取这个的源码,用webclient和webrequest咋都不行呢= =写的是winform的程序 展开
 我来答
lpc_007
2011-05-06 · TA获得超过135个赞
知道小有建树答主
回答量:192
采纳率:0%
帮助的人:202万
展开全部
///GetPageHtml.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;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

public partial class LeadInBlog_GetPageHtml : System.Web.UI.Page
{
private string PageUrl = "";

protected void Page_Load(object sender, EventArgs e)
{

}
private void GetText_Click(object sender, System.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();
ContentHtml.Text = Regex.Replace(ContentHtml.Text, "<[^>]*>", "");
//替换空格
ContentHtml.Text = Regex.Replace(ContentHtml.Text, "\\s+", " ");
}
protected void WebClientButton_Click1(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.UTF8);
ContentHtml.Text = sr.ReadToEnd();
resStream.Close();
sr.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
//根据Url地址得到网页的html源码

string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UrlText.Text);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
catch
{

}
ContentHtml.Text = strResult;
}
}

///GetPageHtml.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetPageHtml.aspx.cs" Inherits="LeadInBlog_GetPageHtml" ValidateRequest="false" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<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://blog.sina.com.cn/rss/xujinglei.xml</asp:TextBox>
<asp:Button id="WebClientButton" Runat="server" Text="用WebClient得到" OnClick="WebClientButton_Click1"></asp:Button>
<asp:Button id="WebRequestButton" runat="server" Text="用WebRequest得到" OnClick="WebRequestButton_Click"></asp:Button>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:TextBox id="ContentHtml" runat="server" Width="100%" Height="360px" TextMode="MultiLine"></asp:TextBox>
</form>
</body>
</html>
yaobing0687
2011-05-06 · TA获得超过988个赞
知道小有建树答主
回答量:1309
采纳率:0%
帮助的人:903万
展开全部
在浏览器上右键->查看源码
或者在浏览器的文件菜单下->将网页另存为
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式