2013-11-26 34 views
6

在C#Windows窗体应用程序,我可以用得到一个网页的内容:获取网页的页面内容和HTTP状态代码在C#

string content = webClient.DownloadString(url); 

我可以用得到的HTTP标头:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.Method = "GET"; 
string response = ((HttpWebResponse)request.GetResponse()).StatusCode.ToString(); 

有没有办法在服务器上一次访问内容和HTTP状态码(如果失败),而不是两次?

谢谢。

+0

呵呵?你使用GET,所以你得到GET。问题在哪里? –

+1

'request.GetResponse()'让你们都得到了。你是那个只从中获取'StatusCode'的人。 – Tobberoth

回答

5

您可以从流读取的数据HttpWebResponse对象中:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.Method = "GET"; 
using (var response = request.GetResponse()) 
using (var stream = response.GetResponseStream()) 
using (var reader = new StreamReader(stream)) 
{ 
    HttpStatusCode statusCode = ((HttpWebResponse)response).StatusCode; 
    string contents = reader.ReadToEnd(); 
} 

这样,你就必须手动检测的编码,或者使用库来检测编码。您也可以从HttpWebResponse对象中读取编码作为字符串,当存在时,它位于ContentType属性内。如果页面是Html,那么您将不得不解析它,以便在文档顶部或头部内部进行可能的编码更改。

读取处理来自ContentType标头编码

var request = (HttpWebRequest)WebRequest.Create(url); 
request.Method = "GET"; 
string content; 
HttpStatusCode statusCode; 
using (var response = request.GetResponse()) 
using (var stream = response.GetResponseStream()) 
{ 
    var contentType = response.ContentType; 
    Encoding encoding = null; 
    if (contentType != null) 
    { 
     var match = Regex.Match(contentType, @"(?<=charset\=).*"); 
     if (match.Success) 
      encoding = Encoding.GetEncoding(match.ToString()); 
    } 

    encoding = encoding ?? Encoding.UTF8; 

    statusCode = ((HttpWebResponse)response).StatusCode; 
    using (var reader = new StreamReader(stream, encoding)) 
     content = reader.ReadToEnd(); 
} 
3

WebClient的

我假设你使用WebClient,因为它很容易的WebRequest到字符串处理。不幸的是,WebClient不公开HTTP响应代码。您可以假设反应是积极的(2xx),除非你得到一个exception and read it

try 
{ 
    string content = webClient.DownloadString(url); 
} 
catch (WebException e) 
{ 
    HttpWebResponse response = (System.Net.HttpWebResponse)we.Response;  
    var statusCode = response.StatusCode; 
} 

或者,如果你在成功的代码,你可以使用反射作为解释here真正感兴趣。


HttpClient的

您也可以使用HttpClient如果你在.NET 4。5,这无疑揭穿了响应代码,as explained here

using (HttpClient client = new HttpClient()) 
{ 
    HttpResponseMessage response = await client.GetAsync(url); 

    string content = await response.Content.ReadAsStringAsync(); 
    var statusCode = response.StatusCode;  
} 

HttpWebRequest的

或者,你可以使用HttpWebRequest要获得状态和响应as explained here

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.Method = "GET"; 
var response = (HttpWebResponse)request.GetResponse(); 

using (Stream stream = response.GetResponseStream()) 
{ 
    StreamReader reader = new StreamReader(stream); 

    string content = reader.ReadToEnd(); 
    var statusCode = response.StatusCode;  
} 
0

我可以得到HTTP头唱歌: request.Method =“GET”;

方法GET返回HEAD和BODY部分作为响应。 HTTP也支持方法HEAD - 仅返回HEAD部分。

您可以使用GetResponseStream method从HttpWebResponse获取BODY。