获取远程网页的内容之一(downmoon原创)
一、本机直接上网时:
获取指定远程网页内容
#region 获取指定远程网页内容
/**/ /// <summary>
/// 获取指定远程网页内容
/// </summary>
/// <paramname="strUrl"> 所要查找的远程网页地址 </param>
/// <paramname="timeout"> 超时时长设置,一般设置为8000 </param>
/// <paramname="enterType"> 是否输出换行符,0不输出,1输出文本框换行 </param>
/// <paramname="EnCodeType"> 编码方式 </param>
/// <returns></returns>
/// 也可考虑staticstring
public string GetRequestString( string strUrl, int timeout, int enterType,EncodingEnCodeType)
... {
string strResult;
try
... {
HttpWebRequestmyReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
myReq.Timeout = timeout;
HttpWebResponseHttpWResp = (HttpWebResponse)myReq.GetResponse();
StreammyStream = HttpWResp.GetResponseStream();
StreamReadersr = new StreamReader(myStream,EnCodeType);
StringBuilderstrBuilder = new StringBuilder();
while ( - 1 != sr.Peek())
... {
strBuilder.Append(sr.ReadLine());
if (enterType == 1 )
... {
strBuilder.Append( " " );
}
}
strResult = strBuilder.ToString();
}
catch (Exceptionerr)
... {
strResult = " 请求错误: " + err.Message;
}
return strResult;
}
#endregion
/**/ /// <summary>
/// 获取指定远程网页内容
/// </summary>
/// <paramname="strUrl"> 所要查找的远程网页地址 </param>
/// <paramname="timeout"> 超时时长设置,一般设置为8000 </param>
/// <paramname="enterType"> 是否输出换行符,0不输出,1输出文本框换行 </param>
/// <paramname="EnCodeType"> 编码方式 </param>
/// <returns></returns>
/// 也可考虑staticstring
public string GetRequestString( string strUrl, int timeout, int enterType,EncodingEnCodeType)
... {
string strResult;
try
... {
HttpWebRequestmyReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
myReq.Timeout = timeout;
HttpWebResponseHttpWResp = (HttpWebResponse)myReq.GetResponse();
StreammyStream = HttpWResp.GetResponseStream();
StreamReadersr = new StreamReader(myStream,EnCodeType);
StringBuilderstrBuilder = new StringBuilder();
while ( - 1 != sr.Peek())
... {
strBuilder.Append(sr.ReadLine());
if (enterType == 1 )
... {
strBuilder.Append( " " );
}
}
strResult = strBuilder.ToString();
}
catch (Exceptionerr)
... {
strResult = " 请求错误: " + err.Message;
}
return strResult;
}
#endregion
二:通过域环境代理上网时这样就不行了! 下篇文章分解,呵呵!
获取远程网页的内容之二(downmoon原创)
在webForm中WebRequest/WebClient/WebBrowser获取远程页面源码的三种方式(downmoon)
<script src="http://blog.csdn.net/count.aspx?ID=663337&Type=Rank" type="text/javascript"></script>