读取HTML页面的SourceCode [英] Read SourceCode of HTML page

查看:73
本文介绍了读取HTML页面的SourceCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的应用程序中,我想显示某个URL中提供的页面描述。



现在我想获取该URL的源代码。



获取SourceCode后,我将其保存到数据库中。



我会随时显示。



我试过了



Hi,

In my application I want to show the description of a page which is available in some URL.

Now I want to get the Sourcecode of that URL.

After getting the SourceCode, I will save it into database.

And I will display whenever I want.

I have tried

Method 1:

WebRequest req = WebRequest.Create("http://www.google.com");
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string html = sr.ReadToEnd();

Method 2:

string address = "http://www.google.com/";

using (WebClient wc = new WebClient())
{
     string content = wc.DownloadString(address);
}





在这两种方法中,我都遇到了错误。无论是超时还是无法远程连接到网站。



请帮助



In both methods I am facing an error. Either it's timed out exception or unable to connect to the website remotely.

Please help

推荐答案

以防万一,我在我的HttpDownloader中试用它,我在这里发布了完整的源代码(只有一个文件):如何从互联网上下载文件 [ ^ ]。



在最棘手的情况下,它对我有用。代码很短,但你可以学习一些有用的技巧。



-SA
Just in case, try it with my HttpDownloader, which I published in full source code (just one file) here: how to download a file from internet[^].

It worked for me in most tricky situations. The code is short, yet you can learn some useful techniques.

—SA


更好你可以使用Webclient类来简化你的任务:



使用System.Net;

使用(WebClient客户端=新的WebClient())

{

string htmlCode = client.DownloadString(http://somesite.com/default.html);

}
Better you can use the Webclient class to simplify your task:

using System.Net;
using (WebClient client = new WebClient())
{
string htmlCode = client.DownloadString(http://somesite.com/default.html);
}


这篇关于读取HTML页面的SourceCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆