HttpClient的当超时设置挂机(Windows手机) [英] HttpClient hangs when timeout is setting (Windows Phone)

查看:201
本文介绍了HttpClient的当超时设置挂机(Windows手机)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图超时设置为的HttpClient 对象的Windows Phone应用程序。但是,如果请求没有超时前完成, GetAsync 从不返回一个值。

I'm trying to set timeout to HttpClient object in Windows Phone App. But if request is not completed before timeout, GetAsync never returns a value.

我用下面的code得到响应:

I'm using the following code to get response:

HttpClientHandler handler = new HttpClientHandler();
HttpClient client = new HttpClient(handler);
client.Timeout = TimeSpan.FromSeconds(5);
client.BaseAddress = new Uri("http://www.foo.com");
HttpResponseMessage response = await client.GetAsync("/boo.mp3");//<--Hangs
byte[] data = await response.Content.ReadAsByteArrayAsync();

如何正确设置超时摆脱GetAsync结果呢?

How can I properly set the timeout to get result from GetAsync?

推荐答案

从<一个拍摄href=\"https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout%28v=vs.110%29.aspx\"相对=nofollow> HttpClient的文档:

默认值为100,000毫秒(100秒)。

The default value is 100,000 milliseconds (100 seconds).

一个域名系统(DNS)查询可能需要长达15秒返回
  或超时。如果您的请求包含需要主机名
  分辨率并设置为超时值小于15秒,它可能
  需要15秒以上引发WebException指示之前
  超时您的要求。

A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.

和ZombieSheep指出5秒钟甚至不够一个DNS查询来完成。

and as ZombieSheep notes 5 seconds are not enough even for a DNS query to complete.

我会建议取消超时,以及让它是默认值,因为据我所知,以检查,如果任务剪掉站是假设,如果你ping服务器的唯一途径,它回复的连接还是OK和工作/下载你的文件。

I would suggest removing the timeout as well and let it to is default value, since from what I know the only way to "check" if task didnt stop is by assuming that if you ping the server and it replies the connection is still "OK" and working/downloading your file.

这篇关于HttpClient的当超时设置挂机(Windows手机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆