【问题标题】:.Net HttpResponseMessage receiving BadGateway when take more than 1min.Net HttpResponseMessage 接收 BadGateway 时需要超过 1 分钟
【发布时间】:2016-04-25 15:08:41
【问题描述】:

我有一个与 PHP Web 服务通信的 Windows 服务 .Net 客户端。基本思路,.Net客户端发送信息到php写入数据库,这个过程需要1分钟左右,问题是,当需要更多的时候,HttpResponseMessage带有BadGateway,有趣的是php实际上把所有的都写了即使需要超过 1 分钟,也要写入数据库。我尝试将 HttpClient 的 TimeOut 设置为 10 分钟:

//code for the stuff making
Logger.WriteLog("Sending Stuff");
using (var client = new HttpClient())
        {
            client.Timeout = new TimeSpan(0,10,0);
            httpResponseMessage = client.PostAsync(url, content).Result;
            Logger.WriteLog(httpResponseMessage.StatusCode.ToString());
            Logger.WriteLog(httpResponseMessage.ToString());                
        }

但是,它仍然以超过 1 分钟的响应失败,它假设的 php Web 服务也有 10 分钟的超时。我记录了完整的响应,但我得到了错误的网关:

2016-04-25 11:12:24,045 INFO  - *Sending Stuff
2016-04-25 11:13:23,413 INFO  - OK
2016-04-25 11:13:23,414 INFO  - StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  Connection: close
  Cache-Control: no-store, must-revalidate, no-cache, post-check=0, pre-check=0
  Date: Mon, 25 Apr 2016 14:12:37 GMT
  Set-Cookie: PHPSESSID=lbhsue3pstc8smqihkoe0nouj1; path=/
  Server: Apache/2.2.15
  Server: (CentOS)
  X-Powered-By: PHP/5.6.19
  Content-Length: 52
  Content-Type: application/json; charset=UTF8
  Expires: Thu, 19 Nov 1981 08:52:00 GMT
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
2016-04-25 11:14:42,931 INFO  - *Sending Stuff
2016-04-25 11:15:44,273 INFO  - BadGateway
2016-04-25 11:15:44,274 INFO  - StatusCode: 502, ReasonPhrase: 'Proxy Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: close
  Date: Mon, 25 Apr 2016 14:14:43 GMT
  Content-Length: 499
  Content-Type: text/html; charset=iso-8859-1
}
//------------------------------------------------------------------------------------------------------------------------------------------------
2016-04-25 11:25:28,471 INFO  - *Sending Stuff
2016-04-25 11:26:27,495 INFO  - OK
2016-04-25 11:26:27,497 INFO  - StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
 Pragma: no-cache
 Connection: close
 Cache-Control: no-store, must-revalidate, no-cache, post-check=0, pre-check=0
 Date: Mon, 25 Apr 2016 14:25:41 GMT
 Set-Cookie: PHPSESSID=ja6f30mqlnhtdl8p44hddr43c3; path=/
 Server: Apache/2.2.15
 Server: (CentOS)
 X-Powered-By: PHP/5.6.19
 Content-Length: 52
 Content-Type: application/json; charset=UTF8
 Expires: Thu, 19 Nov 1981 08:52:00 GMT
}

我的希望是,badgateway 的原因超时,但它没有区别。有人可以告诉我 badgateway 的可能性是什么,即使 php 中的进程完成了任务。提前致谢! PD:对不起我的英语

【问题讨论】:

    标签: php .net windows-services


    【解决方案1】:

    .NET 端的超时不应生成 HTTP 状态代码。它应该抛出一个单独的超时异常。如果您看到 HTTP 状态代码,则该代码必须来自服务器端。

    您确定 Web 服务器本身(在 PHP 端)没有为脚本配置超时吗?

    【讨论】:

    • ....是第 3 方软件,所以我不能 100% 确定。但是,.Net 本身无法做出 badgateway 响应意味着没有可能与我有关? windows 可以终止来自他的服务之一的 http 请求吗?或 .net 框架?
    • 是的,由于多种原因,HTTP 请求可以在客户端终止,但我有 98% 的把握这不会显示为 HTTP 状态代码。 HTTP 状态码只能来自另一端的服务器。
    猜你喜欢
    • 1970-01-01
    • 2019-03-10
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2016-02-13
    相关资源
    最近更新 更多