HttpClient.PostAsJsonAsync永远不会看到发布成功和响应的时间 [英] HttpClient.PostAsJsonAsync never sees when the post is succeeding and responding

查看:76
本文介绍了HttpClient.PostAsJsonAsync永远不会看到发布成功和响应的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用HttpClient将json发布到一个宁静的Web服务.在一种情况下,我们遇到了使我们感到困惑的事情.使用邮递员,提琴手等工具,我们可以将其发布到端点并查看其是否正常运行.当我们对HttpClient.PostAsJsonAsync进行相同操作时,我们可以在要发布的软件中验证它是否可以正确接收数据.但是,我们的PostAsJsonAsync最终总是会超时,而不是给我们响应.

We are using an HttpClient to post json to a restful web service. In one instance, we are running into something that has us baffled. Using tools like postman, fiddler etc, we can post to an endpoint and see that it is working. When we do the same with HttpClient.PostAsJsonAsync, we can verify in the software we are posting to that it received the data just fine. However, our PostAsJsonAsync will always eventually time out rather than give us a response.

我们已经与创建我们正在使用的服务的团队一起工作,并在我们这方面进行了额外的测试,我们还无法真正使该服务超时.

We have worked with the team that created the service we are consuming, plus our additional testing on our side, and we have not yet been able to truly time out that service.

每次我们使用HttpClient进行发布时,我们都可以验证发布到的目标软件确实获得了数据.每当我们从任何其他工具向该目标软件发布内容时,我们总是会很快看到状态码为200的响应.关于HttpClient的某些信息无法接受来自此特定服务的响应.有谁知道我们可以从这里看到什么?

Every time we do a post with HttpClient, we then can verify that the target software we post to does indeed get the data. Any time we do a post to that target software from any other tool, we always very quickly see a response with status code of 200. Something about HttpClient is failing to accept the response from this particular service. Does anyone have an idea what we can look at from here?

这是代码(尽管它是如此的千篇一律,我几乎感觉不到它是必需的)

Here's the code (though it is so cookie cutter that I hardly feel it is needed)

public string PostData(string resourcePath, Object o, Boolean isCompleteUrl = false, int timeoutMinutes = -1)
    {
        using (var client = new HttpClient())
        {
            if (timeoutMinutes > 0)
            {
                client.Timeout = new TimeSpan(0,timeoutMinutes,0);
            }
            var useUrl = isCompleteUrl ? resourcePath : ApiBase + resourcePath;
            var response = client.PostAsJsonAsync(useUrl, o).Result;
            if(response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return response.Content.ReadAsStringAsync().Result;
            }
            return "";
        }

    }

推荐答案

System.Net.ServicePointManager.Expect100Continue = false;

在我们的案例中,那一行代码解决了该问题.来自不同团队的开发人员提出了该建议,并且该建议可行.我还没有在Google上搜索并阅读足够的内容,可以对所要解决的问题进行解释.

That one line of code in our case fixed the problem. A developer from a different team offered that suggestion, and it works. I have yet to google it and read up on it enough to offer an explanation of what that is addressing.

这篇关于HttpClient.PostAsJsonAsync永远不会看到发布成功和响应的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆