dz asp.net论坛中函数--根据Url获得源文件内容
作者:
从asp.net dz论坛发现的这个函数,学习一下高手的经验代码
需要引入命名空间:
using System.Net;
using System.IO;
函数内容:
/// <summary>
/// 根据Url获得源文件内容
/// </summary>
/// <param name="url">合法的Url地址</param>
/// <returns></returns>
public static string GetSourceTextByUrl(string url)
{
WebRequest request = WebRequest.Create(url);
request.Timeout = 20000;//20秒超时
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream);
return sr.ReadToEnd();
}
复制代码 代码如下:
using System.Net;
using System.IO;
函数内容:
复制代码 代码如下:
/// <summary>
/// 根据Url获得源文件内容
/// </summary>
/// <param name="url">合法的Url地址</param>
/// <returns></returns>
public static string GetSourceTextByUrl(string url)
{
WebRequest request = WebRequest.Create(url);
request.Timeout = 20000;//20秒超时
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream);
return sr.ReadToEnd();
}