HttpClient GetStreamAsync 和 HTTP 状态代码?
HttpClient GetStreamAsync and HTTP status codes?
我希望使用 json.net performance tips documentation, however I'm unable to find how to get a hold of the http status codes without the typical awaiting the HttpResponse 推荐的流。
有没有办法先获取状态码而不读取数据?所以还在利用流?
我还没有测试以确保它的性能,但这看起来很有希望:
using(HttpClient client = new HttpClient())
{
var response = await client.GetAsync("http://httpbin.org/get", HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using (var stream = await response.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
//do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
}
}
我更喜欢通过 using
处理 HttpResponseMessage,因为它是一次性的。我也不想依赖异常处理来处理失败的请求。相反,我更喜欢检查 IsSuccessStatusCode
布尔值并相应地进行。例如:
using(HttpClient client = new HttpClient())
{
using(var response = await client.GetAsync("http://httpbin.org/get", HttpCompletionOption.ResponseHeadersRead))
{
if(response.IsSuccessStatusCode)
{
using (var stream = await response.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
//do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
}
}
else {
//do your error logging and/or retry logic
}
}
}
EDIT: If you're doing work with a rate limited api sending the HEAD request just isn't sometimes feasible. As such, here's a code sample using the good ol' fashion HttpWebRequest
(note that there isn't a better way to deal with http errors than WebException
in this case):
var req = WebRequest.CreateHttp("http://httpbin.org/get");
/*
* execute
*/
try
{
using (var resp = await req.GetResponseAsync())
{
using (var s = resp.GetResponseStream())
using (var sr = new StreamReader(s))
using (var j = new JsonTextReader(sr))
{
var serializer = new JsonSerializer();
//do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
}
}
}
catch (WebException ex)
{
using (HttpWebResponse response = (HttpWebResponse)ex.Response)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string respStr = sr.ReadToEnd();
int statusCode = (int)response.StatusCode;
//do your status code logic here
}
}
}
我希望使用 json.net performance tips documentation, however I'm unable to find how to get a hold of the http status codes without the typical awaiting the HttpResponse 推荐的流。
有没有办法先获取状态码而不读取数据?所以还在利用流?
我还没有测试以确保它的性能,但这看起来很有希望:
using(HttpClient client = new HttpClient())
{
var response = await client.GetAsync("http://httpbin.org/get", HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using (var stream = await response.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
//do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
}
}
我更喜欢通过 using
处理 HttpResponseMessage,因为它是一次性的。我也不想依赖异常处理来处理失败的请求。相反,我更喜欢检查 IsSuccessStatusCode
布尔值并相应地进行。例如:
using(HttpClient client = new HttpClient())
{
using(var response = await client.GetAsync("http://httpbin.org/get", HttpCompletionOption.ResponseHeadersRead))
{
if(response.IsSuccessStatusCode)
{
using (var stream = await response.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
//do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
}
}
else {
//do your error logging and/or retry logic
}
}
}
EDIT: If you're doing work with a rate limited api sending the HEAD request just isn't sometimes feasible. As such, here's a code sample using the good ol' fashion
HttpWebRequest
(note that there isn't a better way to deal with http errors thanWebException
in this case):
var req = WebRequest.CreateHttp("http://httpbin.org/get");
/*
* execute
*/
try
{
using (var resp = await req.GetResponseAsync())
{
using (var s = resp.GetResponseStream())
using (var sr = new StreamReader(s))
using (var j = new JsonTextReader(sr))
{
var serializer = new JsonSerializer();
//do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
}
}
}
catch (WebException ex)
{
using (HttpWebResponse response = (HttpWebResponse)ex.Response)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string respStr = sr.ReadToEnd();
int statusCode = (int)response.StatusCode;
//do your status code logic here
}
}
}