- public static string GetwebPageAsString(string url)
- {
- HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
- HttpWebResponse httpWebResponse = null;
- string xml = "";
- try
- {
- httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse();
- }
- catch (WebException exception)
- {
- if (exception.Status == WebExceptionStatus.ProtocolError)
- { //get the response object from the WebException
- httpWebResponse = exception.Response as HttpWebResponse;
- if (httpWebResponse == null){ return "<Error />";}
- }
- }
- Stream stream = httpWebResponse.GetResponseStream();
- StreamReader streamReader = new StreamReader(stream, Encoding.ASCII);
- xml = streamReader.ReadToEnd();
- //streamReader.Close();
- if (httpWebResponse.StatusCode != System.Net.HttpStatusCode.OK)
- {
- throw new Exception(xml);
- }
- return xml;
- }
- //该片段来自于http://www.codesnippet.cn/detail/090620133964.html
来源: http://www.codesnippet.cn/detail/090620133964.html