方法一,适用于获取少量数据:
清测试使用发现,这个方法在获取有很多内容的网页时,会出现只获取一部分网页内容,底部内容显示不全的情况
Code
/// <summary>
/// 获取远程服务器结果,适用于获取少量内容
/// </summary>
/// <param name="a_strUrl">远程url地址</param>
/// <param name="timeout">超时时间,以毫秒为单位</param>
/// <param name="newline">结果是否需要换行</param>
/// <returns></returns>
public static String GetHttp(String a_strUrl, int timeout, bool newline)
{
string strResult="";
timeout = timeout == 0 ? 1000 : timeout;
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
myReq.Timeout = timeout;
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine());
if(newline)
strBuilder.Append("\r\n");
}
strResult = strBuilder.ToString();
myReq = null;
HttpWResp.Close();
myStream.Close();
myStream.Close();
sr.Close();
strBuilder = null;
}
catch (Exception exp)
{
#if DEBUG
strResult = "GetHttp错误:" + exp.Message;
#else
strResult = "time out";
#endif
}
return strResult;
}
/// <summary>
/// 获取远程服务器结果,适用于获取少量内容
/// </summary>
/// <param name="a_strUrl">远程url地址</param>
/// <param name="timeout">超时时间,以毫秒为单位</param>
/// <param name="newline">结果是否需要换行</param>
/// <returns></returns>
public static String GetHttp(String a_strUrl, int timeout, bool newline)
{
string strResult="";
timeout = timeout == 0 ? 1000 : timeout;
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
myReq.Timeout = timeout;
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine());
if(newline)
strBuilder.Append("\r\n");
}
strResult = strBuilder.ToString();
myReq = null;
HttpWResp.Close();
myStream.Close();
myStream.Close();
sr.Close();
strBuilder = null;
}
catch (Exception exp)
{
#if DEBUG
strResult = "GetHttp错误:" + exp.Message;
#else
strResult = "time out";
#endif
}
return strResult;
}
方法二,适用于获取大量数据:
Code
/// <summary>
/// 获取远程URL内容,适用于获取大量内容
/// </summary>
/// <param name="a_strUrl">远程URL地址</param>
/// <param name="encodetype">目标URL的编码方式</param>
/// <returns></returns>
public static string GetHttp(String a_strUrl,string encodetype)
{
string result = "";
if(encodetype.IsNull())
encodetype = "GB2312";
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials; //获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
Byte[] pageData = MyWebClient.DownloadData(a_strUrl);
result = Encoding.GetEncoding(encodetype).GetString(pageData);
return result;
}
/// <summary>
/// 获取远程URL内容,适用于获取大量内容
/// </summary>
/// <param name="a_strUrl">远程URL地址</param>
/// <param name="encodetype">目标URL的编码方式</param>
/// <returns></returns>
public static string GetHttp(String a_strUrl,string encodetype)
{
string result = "";
if(encodetype.IsNull())
encodetype = "GB2312";
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials; //获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
Byte[] pageData = MyWebClient.DownloadData(a_strUrl);
result = Encoding.GetEncoding(encodetype).GetString(pageData);
return result;
}