- // Create a request for the URL.
- webRequest request = WebRequest.Create("http://www.baidu.com/");
- // If required by the server, set the credentials.
- request.Credentials = CredentialCache.DefaultCredentials;
- // Get the response.
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- // Display the status.
- Response.Write(response.StatusDescription);
- // Get the stream containing content returned by the server.
- Stream dataStream = response.GetResponseStream();
- // Open the stream using a StreamReader for easy access.
- StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
- // Read the content.
- string responseFromServer = reader.ReadToEnd();
- // Display the content.
- Response.Write(responseFromServer);
- // Cleanup the streams and the response.
- reader.Close();
- dataStream.Close();
- response.Close();
- //该片段来自于http://www.codesnippet.cn/detail/2310201513875.html
来源: http://www.codesnippet.cn/detail/2310201513875.html