获取远程WEB主机HTTP页面(C#)
[ 2006-05-13 00:41:22 | 作者: admin ]
大部分代码参考.Text
using System;
using System.Net;
using System.IO;
using System.Text;
namespace test
{
public class HttpServerRequest
{
private const int defaultTimeout_ = 60000;
private static string referer_ = @"http://www.vckbase.com";
private int _nTimeout = 0;
public int Timeout
{
get
{
return _nTimeout;
}
set
{
_nTimeout = value;
}
}
public HttpWebRequest CreateRequest(string url)
{
return CreateRequest(url,referer_);
}
public HttpWebRequest CreateRequest(string url, string referral)
{
WebRequest req = WebRequest.Create(url);
HttpWebRequest wreq = req as HttpWebRequest;
if (null != wreq)
{
wreq.UserAgent = "Mozilla/4.0"; // 不添加UserAgent可能无法穿透某些防火墙
wreq.Referer = referral;
wreq.Timeout = (_nTimeout > 0)? _nTimeout : defaultTimeout_;
}
return wreq;
}
public HttpWebResponse GetResponse(string url)
{
return GetResponse(url,referer_);
}
public HttpWebResponse GetResponse(string url, string referral)
{
HttpWebRequest request = CreateRequest(url);
return (HttpWebResponse)request.GetResponse() ;
}
public string GetPageText(string url)
{
return GetPageText(url,referer_);
}
public string GetPageText(string url, string referral)
{
string str;
HttpWebResponse response = GetResponse(url);
using (Stream s = response.GetResponseStream())
{
string enc = response.ContentEncoding.Trim() ;
if ( enc == "" )
enc = "gb2312"; //enc = "us-ascii";
Encoding encode = System.Text.Encoding.GetEncoding(enc);
using ( StreamReader sr = new StreamReader( s, encode ) )
{
str=sr.ReadToEnd() ;
}
}
response.Close();
return str;
}
}
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
HttpServerRequest ServerRequest = new HttpServerRequest();
ServerRequest.Timeout = 16000; // 超时16秒
string pageText = ServerRequest.GetPageText("http://www.google.com/");
Console.WriteLine(pageText);
}
catch(System.Net.WebException ex)
{
Console.WriteLine("出错:"+ex.Message);
}
catch(Exception ex)
{
Console.WriteLine("出错:"+ex.Message);
}
}
}
}posted on 2005-05-18 10:44 王骏
using System;
using System.Net;
using System.IO;
using System.Text;
namespace test
{
public class HttpServerRequest
{
private const int defaultTimeout_ = 60000;
private static string referer_ = @"http://www.vckbase.com";
private int _nTimeout = 0;
public int Timeout
{
get
{
return _nTimeout;
}
set
{
_nTimeout = value;
}
}
public HttpWebRequest CreateRequest(string url)
{
return CreateRequest(url,referer_);
}
public HttpWebRequest CreateRequest(string url, string referral)
{
WebRequest req = WebRequest.Create(url);
HttpWebRequest wreq = req as HttpWebRequest;
if (null != wreq)
{
wreq.UserAgent = "Mozilla/4.0"; // 不添加UserAgent可能无法穿透某些防火墙
wreq.Referer = referral;
wreq.Timeout = (_nTimeout > 0)? _nTimeout : defaultTimeout_;
}
return wreq;
}
public HttpWebResponse GetResponse(string url)
{
return GetResponse(url,referer_);
}
public HttpWebResponse GetResponse(string url, string referral)
{
HttpWebRequest request = CreateRequest(url);
return (HttpWebResponse)request.GetResponse() ;
}
public string GetPageText(string url)
{
return GetPageText(url,referer_);
}
public string GetPageText(string url, string referral)
{
string str;
HttpWebResponse response = GetResponse(url);
using (Stream s = response.GetResponseStream())
{
string enc = response.ContentEncoding.Trim() ;
if ( enc == "" )
enc = "gb2312"; //enc = "us-ascii";
Encoding encode = System.Text.Encoding.GetEncoding(enc);
using ( StreamReader sr = new StreamReader( s, encode ) )
{
str=sr.ReadToEnd() ;
}
}
response.Close();
return str;
}
}
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
HttpServerRequest ServerRequest = new HttpServerRequest();
ServerRequest.Timeout = 16000; // 超时16秒
string pageText = ServerRequest.GetPageText("http://www.google.com/");
Console.WriteLine(pageText);
}
catch(System.Net.WebException ex)
{
Console.WriteLine("出错:"+ex.Message);
}
catch(Exception ex)
{
Console.WriteLine("出错:"+ex.Message);
}
}
}
}
[最后修改由 admin, 于 2006-05-14 17:42:05]
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=574
这篇日志没有评论。
此日志不可发表评论。