从asp.net代码获取一些网站的来源

有没有什么办法可以获得一个网站的来源(最好是一个字符串),让我们说www.google.com,来自asp.net网站背后代码中的一些c#代码?

编辑:当然我的意思是html代码 – 在每个浏览器中,你可以使用上下文菜单中的“查看源代码 ”查看它。

假设您要检索html:

class Program { static void Main(string[] args) { using (WebClient client = new WebClient()) using (Stream stream = client.OpenRead("http://www.google.com")) using (StreamReader reader = new StreamReader(stream)) { Console.WriteLine(reader.ReadToEnd()); } } } 

对于C#,我更喜欢在WebClient上使用HttpWebRequest ,因为您可以在将来拥有更多选项,例如使用GET / POST参数,使用Cookie等。

您可以在MSDN上进行最短的解释。

以下是MSDN的示例:

  // Create a new HttpWebRequest object. HttpWebRequest request=(HttpWebRequest) WebRequest.Create("http://www.contoso.com/example.aspx"); // Set the ContentType property. request.ContentType="application/x-www-form-urlencoded"; // Set the Method property to 'POST' to post data to the URI. request.Method = "POST"; // Start the asynchronous operation. request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request); // Keep the main thread from continuing while the asynchronous // operation completes. A real world application // could do something useful such as updating its user interface. allDone.WaitOne(); // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); string responseString = streamRead.ReadToEnd(); Console.WriteLine(responseString); // Close the stream object. streamResponse.Close(); streamRead.Close(); // Release the HttpWebResponse. response.Close(); 

它不是最明显(也是最好)的方式,但我发现在Windows窗体中你可以使用WebBrowser控件(如果你真的需要它),用你需要的url填充它的Url属性,当它加载时,读取DocumentText属性 – 它包含所查看网站的html代码。