如何获取页面源?

时间:2011-07-06 13:10:12

标签: c# http

如何在C#中获取页面源代码(例如:html,aspx,php)?

像这样:

<head>

<title>Ask a Question - Stack Overflow</title>
<link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
<link rel="apple-touch-icon" href="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">
<link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">

3 个答案:

答案 0 :(得分:1)

最简单的方法是创建WebClient对象并调用其DownloadString方法,但如果除了简单的请求/响应之外还需要其他任何内容,则可能必须使用HttpWebRequest来制定请求

答案 1 :(得分:0)

不完全确定要问的是什么,但是你不是只想打开到网络服务器的正确端口(通常为80)的tcp连接,写一个正确种类的“POST”然后读取响应数据?

如果是这样,请查找C#

中提供的http帮助程序类/库

答案 2 :(得分:0)

您可以使用httpwebrequest类或webclient类,它位于system.net名称空间中。

WebClient client = new WebClient();
UTF8Encoding encoding1 = new UTF8Encoding();
byte[] downloadDataInBytes = client.DownloadData("http://negaweblog.wordpress.com");
string websitesource = encoding1.GetString(downloadDataInBytes);
相关问题