asp.net(C#) 远程获取网页内容代码分享

//方法一:
// Create a request for the URL.
WebRequest request = WebRequest.Create("http://www.hao123.com");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
Response.Write(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream,Encoding.Default);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Response.Write(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();


//方法二:
WebClient client = new WebClient();

// Add a user agent header in case the
// requested URI contains a query.

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead("http://www.hao123.com");
StreamReader reader = new StreamReader(data,Encoding.Default);
string s = reader.ReadToEnd();
Response.Write(s);
data.Close();
reader.Close();

//方法三:
WebClient client = new WebClient();
//client.DownloadFile("http://www.hao123.com","123.htm");
string reply = client.DownloadString("http://www.hao123.com");
Response.Write(reply);



//方法四:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace thief
{
class Program
{
static void Main(string[] args)
{

try {
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
Byte[] pageData = MyWebClient.DownloadData("http://www.163.com");//从指定网站下载数据
string pageHtml = Encoding.Default.GetString(pageData); //如果获取网站页面采用的是GB2312,则使用这句
//string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句
Console.WriteLine(pageHtml);//在控制台输入获取的内容
using (StreamWriter sw = new StreamWriter("c:\\test\\ouput.html"))//将获取的内容写入文本
{
sw.Write(pageHtml);
}
Console.ReadLine(); //让控制台暂停,否则一闪而过了
}
catch(WebException webEx) {
Console.WriteLine(webEx.Message.ToString());
}
}
}
}

你可能感兴趣的:(asp.net)