System.Net.Http.HttpClient超时似乎被忽略了
本文关键字:被忽略了 超时 HttpClient Net Http System | 更新日期: 2023-09-27 18:12:54
我正在使用Xamarin。iOS版本:8.10.5.26(独立版),面对一个非常奇怪的行为与HttpClient()发送超时请求:
下面的代码尝试从url获取结果,并且有60秒的超时(1分钟),但是当请求被触发时,它需要大约90秒的超时。当通话时,我手动关闭网络连接来检查超时时间。据观察,该过程耗时超过60秒。
public async Task<Dictionary<string,object>> GetPatientDataASync (string lUsername)
{
var lDict = new Dictionary<string,object> ();
try {
string lQuerystring = "{Email: '" + lUsername + "'}";
String lUrl = String.Format (Constants.mURLPatient + "?where={0}", JObject.Parse (lQuerystring));
var lClient = new HttpClient ();
lClient.BaseAddress = new Uri (lUrl);
lClient.DefaultRequestHeaders
.Accept
.Add (new MediaTypeWithQualityHeaderValue ("application/json"));
lClient.DefaultRequestHeaders.Add ("X-Parse-Application-Id", Constants.mKeyParseAppId);
lClient.DefaultRequestHeaders.Add ("X-Parse-REST-API-Key", Constants.mKeyRestAPIKey);
lClient.Timeout = new TimeSpan (0, 1, 0);
var request = new HttpRequestMessage ();
request.Method = HttpMethod.Get;
if (Utility.isNetworkConnected ()) {
bool responseStatus = false;
await lClient.SendAsync (request)
.ContinueWith (responseTask => {
if (responseTask != null) {
var response = responseTask.Result;
if (response != null) {
if (response.IsSuccessStatusCode) {
var responseContent = response.Content;
if (responseContent != null) {
string responseString = responseContent.ReadAsStringAsync ().Result;
if (!string.IsNullOrWhiteSpace (responseString)) {
JObject json = JObject.Parse (responseString);
if (json != null) {
if (json ["results"].Any ()) {
Patient user = Patient.Instance;
user.objectId = json.SelectToken (@"results[0].objectId").Value<string> ();
user.Email = json.SelectToken (@"results[0].Email").Value<string> ();
user.Name = json.SelectToken (@"results[0].Name").Value<string> ();
user.IsNotificationsEnabled = json.SelectToken (@"results[0].IsNotificationsEnabled").Value<string> ();
Application.Current.Properties ["IsNotificationsEnabled"] = json.SelectToken (@"results[0].IsNotificationsEnabled").Value<string> ();
if (json.SelectToken (@"results[0].DeviceToken") != null) {
var deviceToken = json.SelectToken (@"results[0].DeviceToken").Value<JArray> ();
if (deviceToken != null)
user.DeviceToken = deviceToken.ToObject < List<string>> ();
} else {
user.DeviceToken = new List<string> ();
}
var doctors = json.SelectToken (@"results[0].MyDoctors").Value<JArray> ();
user.AllergicTo = json.SelectToken (@"results[0].AllergicTo").Value<string> ();
user.ContactNo = json.SelectToken (@"results[0].ContactNo").Value<string> ();
user.BloodGroup = json.SelectToken (@"results[0].BloodGroup").Value<string> ();
user.MyDoctors = doctors != null ? doctors.ToObject<List<string>> () : new List<string> ();
responseStatus = true;
} else
responseStatus = false;
}
}
}
}
}
}
});
lDict.Add (SUCCESS_CODE, responseStatus);
return lDict;
} else {
lDict.Add (NO_INTERNET, Constants.mStringNoInternetMessage);
return lDict;
}
} catch (Exception e) {
Debug.WriteLine (e.Message + "'n " + e.StackTrace);
lDict.Add (EXCEPTION_OCCURED, e);
return lDict;
}
}
如果我的代码有错误,请告诉我。同样的问题也报告在这里:-
第一个链接
第二个链接
这是一个已知的错误,多年来被打开,关闭和重新打开了几次。
有一个变通的解决方案:
- 定义
CancellationTokenSource
并设置Token
为http请求; - 在
CancellationTokenSource
上调用cancel by timeout,cts.CancelAfter(timeout);
: - 不要忘记catch exception,像这样。
try
{
}
catch(TaskCanceledException)
{
if(!cts.Token.IsCancellationRequested)
{// timeout
}
else
{//other reason
}
}