简体   繁体   English

Xamarin HttpClient:TaskCancelledException

[英]Xamarin HttpClient: TaskCancelledException

I using HttpClient to send post request on local server (powered on LAMP or MAMP, tried both) but can't get answer, always getting "Task cancelled exception" with following code 我使用HttpClient在本地服务器上发送发布请求(在LAMP或MAMP上打开电源,尝试了两者),但无法获取答案,始终使用以下代码获取“任务已取消异常”

try
{
    using (HttpClient client = new HttpClient())
    {
        client.Timeout = new TimeSpan(0, 0, 10);
        var sendContent = new StringContent(serialized);

        using (HttpResponseMessage response = await client.PostAsync(url.ToString(), sendContent))
        {
            if (response.StatusCode != HttpStatusCode.OK)
                return MakeError("Bad status: " + response.StatusCode.ToString());

            using (HttpContent content = response.Content)
            {
                string str = await content.ReadAsStringAsync();
                if (str == null)
                    return MakeError("Got null answer");

                App.Log("Response: " + str);
                return str;
            }
        }
    }
}
catch (Exception e)
{
    App.Log("There is something bad with request: " + serialized + " the error was " + e.Message + " url = " + url.ToString());

    return MakeError("Timed out");
}

the url is right, if I trying to execute this code on C# Console Application I can get answer (but not with Xamarin, both Android and iOS, on device and emulators). 如果我尝试在C#控制台应用程序上执行此代码,则该URL是正确的,我可以得到答案(但不能在设备和仿真器上同时使用Xamarin,Android和iOS)。

I also tried to sniff HTTP packets, and I saw that answer was sent by my local server, but Xamarin didn't handled this right way. 我也尝试嗅探HTTP数据包,但看到该答案是由本地服务器发送的,但是Xamarin却没有以这种正确的方式处理。 BUT, if replace url with domain (for example http://stackoverflow.com ) I can get answer. 但是,如果将URL替换为域(例如http://stackoverflow.com ),我可以得到答案。

Following HTTP answer headers: 以下HTTP答案标头:

  1. Connection →close (keep-alive doesn't works too) 连接→关闭(保持活动状态也不起作用)
  2. Content-Length →951 内容长度→951
  3. Content-Type →application/json 内容类型→application / json
  4. Date →Thu, 16 Jun 2016 18:11:40 GMT 日期→2016年6月16日星期四18:11:40 GMT
  5. Server →Apache 服务器→Apache
  6. X-Powered-By →PHP/5.5.14 X-Powered-By→PHP / 5.5.14

Any suggestions? 有什么建议么?

One possible issue is your 8 second timeout. 一个可能的问题是您的8秒超时。

From HttpClient documentation : HttpClient文档中

The default value is 100,000 milliseconds (100 seconds). 默认值为100,000毫秒(100秒)。

A Domain Name System (DNS) query may take up to 15 seconds to return or time out. 域名系统(DNS)查询最多可能需要15秒才能返回或超时。 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. 如果您的请求包含需要解析的主机名,并且您将Timeout设置为小于15秒的值,则可能需要15秒或更长时间才能引发WebException来指示您的请求超时。

So 15 seconds at least are needed for the DNS query if that is taking place in your call. 因此,如果您的通话中发生DNS查询,至少需要15秒。 Remove the timeout and see if the issue is still there. 删除超时,然后查看问题是否仍然存在。

I had the same issue. 我遇到过同样的问题。 i have added following line, i was able to get the response. 我添加了以下行,我能够得到响应。

httpClient.DefaultRequestHeaders.ConnectionClose = true;

just try. 你试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM