Windows phone 8 使用webclient下载JSON时如何处理http 400和401错误
我使用的api在URL无效时返回错误400,在每日qouta耗尽50%时返回错误401。它还返回json,但我无法下载此json,因为如果发生这些错误,会发生异常。我使用的api是 我现在使用的代码是Windows phone 8 使用webclient下载JSON时如何处理http 400和401错误,windows-phone-8,Windows Phone 8,我使用的api在URL无效时返回错误400,在每日qouta耗尽50%时返回错误401。它还返回json,但我无法下载此json,因为如果发生这些错误,会发生异常。我使用的api是 我现在使用的代码是 private void _download_serialized_json_data(Uri Url) { var webClient = new WebClient(); var json_data = string.E
private void _download_serialized_json_data(Uri Url)
{
var webClient = new WebClient();
var json_data = string.Empty;
// attempt to download JSON data as a string
try
{
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(Url);
}
catch (Exception) { }
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
String Json = null;
try
{
Json = e.Result;
}
catch (Exception ex)
{
}
if(Json!=null)
{
data=JsonConvert.DeserializeObject<RootObject>(Json);
result.Text = "facebook : "+data.Facebook.like_count+"\nGooglePlus : "+data.GooglePlusOne;
}
else
{
result.Text = "Invald URL \nor you exceeded your daily quota of 100,000 queries by 50%.";
}
}
private void\u下载\u序列化的\u json\u数据(Uri Url)
{
var webClient=新的webClient();
var json_data=string.Empty;
//尝试以字符串形式下载JSON数据
尝试
{
webClient.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(webClient\u DownloadStringCompleted);
webClient.DownloadStringAsync(Url);
}
捕获(异常){}
}
void webClient_DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
字符串Json=null;
尝试
{
Json=e.结果;
}
捕获(例外情况除外)
{
}
if(Json!=null)
{
data=JsonConvert.DeserializeObject(Json);
result.Text=“facebook:+data.facebook.like_count+”\nGooglePlus:+data.GooglePlusOne;
}
其他的
{
result.Text=“Invald URL\n或者您的每日查询配额超过100000次的50%。”;
}
}
如果发生异常,当前显示两个错误。但是我想下载json并显示它。我该怎么做你可以试试这样的
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
String Json = null;
if(e.Error != null)
{
//Some error occured, show the error message
var ErrorMsg = e.Error.Message;
}
else
{
//Got some response .. rest of your code
Json = e.Result;
}
}
要获取响应内容,您需要使用
System.Net.Http.HttpClient
。从此处安装:
然后试试这个:
private async void Foo2()
{
Uri uri = new Uri("http://localhost/fooooo");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(uri);
HttpStatusCode statusCode = response.StatusCode; // E.g.: 404
string reason = response.ReasonPhrase; // E.g.: Not Found
string jsonString = await response.Content.ReadAsStringAsync(); // The response content.
}
我使用WebClient遇到了同样的问题,我看到Fiddler中捕获了错误响应流,但我的.NET代码捕获了异常,似乎没有捕获响应流 您可以从WebException对象读取响应流以获取响应数据流
using (System.Net.WebClient client = new System.Net.WebClient())
{
string response = "";
try
{
response = client.UploadString(someURL, "user=billy&pass=12345");
}
catch(WebException ex)
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(ex.Response.GetResponseStream()))
{
string exResponse = sr.ReadToEnd();
Console.WriteLine(exResponse);
}
}
}
不,实际上我想知道这是错误400还是401,或者我想读取json,不管错误响应是什么。