下载网页内容存入字符串

 1/// <summary>
 2
 3/// Downloads a web page from the Internet and returns the HTML as a string. .
 4
 5/// </summary>
 6
 7/// <param name="url">The URL to download from.</param>
 8
 9/// <returns>The HTML or null if the URL isn't valid.</returns>

10
11public static string DownloadWebPage(Uri url)
12
13{
14
15  try
16
17  {
18
19    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
20
21    request.Headers["Accept-Encoding"= "gzip";
22
23    request.Headers["Accept-Language"= "en-us";
24
25    request.Credentials = CredentialCache.DefaultNetworkCredentials;
26
27    request.AutomaticDecompression = DecompressionMethods.GZip;
28
29 
30
31    using (WebResponse response = request.GetResponse())
32
33    {
34
35      using (StreamReader reader = new StreamReader(response.GetResponseStream()))
36
37      {
38
39        return reader.ReadToEnd();
40
41      }

42
43    }

44
45  }

46
47  catch (Exception)
48
49  {
50
51    return null;
52
53  }

54
55}

56
posted @ 2008-09-15 17:23  花香的蜂  阅读(247)  评论(0编辑  收藏  举报