c#httpclient PostAsJson发送GET请求而不是POST [英] c# httpclient PostAsJson sending GET request instead of POST

查看:1922
本文介绍了c#httpclient PostAsJson发送GET请求而不是POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HttpClient发布帖子请求。我不回来405方法。在fiddler中捕获跟踪时,它会以GET而不是POST结束!

I am using HttpClient to make a post request. I get back 405 method not allowed. When capturing a trace in fiddler, it goes out as GET instead of POST!

using (var client = new HttpClient())
            {
                var url = AppSettingsUtil.GetString("url");
                var response = client.PostAsJsonAsync(url, transaction).Result;
            }

我知道async / await问题。这是一个简化的示例来说明问题。

I am aware of the async/await issues. This is a simplified sample to show the issue.

是否存在可能影响此问题的某种web.config或machine.config设置?其他请求(通过RestSharp发送)正确发送帖子

Is there some sort of web.config or machine.config setting that could be affecting this? Other requests (sent through RestSharp) send Posts correctly

这是fiddler捕获的内容。在fiddler中重新运行跟踪也会返回405(正如预期的那样)。手动将其切换到POST并从fiddler运行。

Here is what fiddler captures. Rerunning the trace in fiddler also returns the 405 (as expected). Manually switching it to POST and running works from fiddler.

另外,也许是因为方法切换到GET,fiddler中没有捕获身体,我不得不手动粘贴JSON

Also, perhaps because the method was switched to GET, there is no body captured in fiddler, I had to manually paste in the JSON

GET /*URL*/ HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: /*host*/
Connection: Keep-Alive


推荐答案

问题似乎是有人在没有告诉我们的情况下更改了URL,并且他们进行了重定向。 HttpClient响应重定向,但最终实际上将请求作为Get发送到最终目的地。

The problem appears to be that someone changed the URL without telling us, and they put a redirect in place. HttpClient is responding to the redirect, but ends up actually sending the request to the final destination as a Get.

这似乎是HttpClient中的一个错误,它应该是要么发送最终请求作为帖子,要么抛出一个例外,说它不能做我要求的。

This seems like a bug in HttpClient to me, that it should either send the ultimate request as a Post, or throw an exception saying it can't do what I asked it to.

这篇关于c#httpclient PostAsJson发送GET请求而不是POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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