从C#代码发送时,HTTP请求挂起

时间:2018-05-27 13:22:43

标签: c# rest http fiddler

今天我遇到了以下代码的问题,该代码向我的网络服务发出简单的POST请求:

class Program
{
    static void Main(string[] args)
    {
        var response = new HttpClient().PostAsync("http://localhost/myservice",
            new StringContent("{ \"type\": \"confirmation\" }", Encoding.UTF8, "application/json"))
            .Result;
        Console.WriteLine(response.StatusCode);
    }
}

出于某种奇怪的原因,它会持续很长时间。有时它几乎永远挂起,有时它会返回0作为状态代码。 如果我使用邮递员向我的网络服务发送相同的请求,我几乎立即得到200 OK。

重要说明:

  1. 它与RestSharp一样(尝试同步和异步版本):

    var client = new RestClient("http://localhost/myservice");
    var request = new RestRequest("api") { Method = Method.POST };
    request.AddJsonBody(new { type = "confirmation" });
    
    var response = client.Execute(request);
    
  2. 在xUnit而不是Console App下工作相同(最初这段代码是在单元测试中)

  3. 这不是防火墙问题。我已将应用程序添加到防火墙规则中。没有成功。
  4. 试图重启机器。没有成功。
  5. 在IIS Express中尝试使用IIS 10。没有成功。
  6. 环境:.NET 4.7,Windows 10,没有自定义防火墙/反恶意软件

    任何建议都将受到赞赏。

    重要补充: 如果Fiddler打开,它会完美运行,如果Fiddler没有运行,它将停止工作。

0 个答案:

没有答案