今天我遇到了以下代码的问题,该代码向我的网络服务发出简单的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。
重要说明:
它与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);
在xUnit而不是Console App下工作相同(最初这段代码是在单元测试中)
环境:.NET 4.7,Windows 10,没有自定义防火墙/反恶意软件
任何建议都将受到赞赏。
重要补充: 如果Fiddler打开,它会完美运行,如果Fiddler没有运行,它将停止工作。