需要引入命名空間:
復制代碼 代碼如下:
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();
}