[英]Why does HttpClient.PostAsync appear to be sending the request as a GET rather than POST?
我正在尝试使用没有带有PostAsync
方法的主体的 HttpClient发送 POST 请求。 但是,在请求中不断失败,并出现405 Method Not Allowed
。 显然,请求是作为 GET 请求而不是 POST 发送的(如下图所示)。
代码示例:
var response = await new HttpClient().PostAsync("https://api.creativecommons.engineering/v1/auth_tokens/token?client_id=adsf&client_secret=asdf&grant_type=client_credentials", null);
Console.WriteLine(response.RequestMessage);
产生:
Method: GET, RequestUri: 'https://api.creativecommons.engineering/v1/auth_tokens/token/?client_id=adsf&client_secret=asdf&grant_type=client_credentials', Version: 1.1
我也尝试过使用 Flurl,但导致了相同的结果(我猜它是在后台使用 HttpClient)。
为什么PostAsync
将请求作为 GET 而不是 POST 发送?
由于这个问题在没有提供一点建设性批评的情况下引起了如此多的仇恨,所以 StackOverflow 做得很好,做得很好。
如果您遇到类似的问题,请注意以下两点:
这两件事结合起来,看起来好像请求是作为 GET 发送的,而实际上,它是一个 POST 请求,带有 301 响应,并自动重定向为 GET。 在某些情况下可能很明显,有点不幸的是,在我的情况下,差异是 URL 中的一个斜线。
在使用集成测试时,由于 https 在默认启动包含此redirect
时,您将遇到此问题
var opt = new RewriteOptions().AddRedirectToHttps();
app.UseRewriter(opt);
app.UseHttpsRedirection();
仅使用 Microsoft 的CustomWebApplicationFactory
建议是不够的。 您必须在 *Fixture 中配置自己的 TestServer,示例如下
const string baseUrl = "https://localhost:5001";
const string environmentName = "Test";
var contentRoot = Environment.CurrentDirectory;
Configuration = new ConfigurationBuilder()
.SetBasePath(contentRoot)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environmentName}.json", true)
.AddEnvironmentVariables()
.Build();
var builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseContentRoot(contentRoot)
.UseEnvironment(environmentName)
.UseConfiguration(Configuration)
.UseStartup<TestStartup>();
Server = new TestServer(builder) {BaseAddress = new Uri(baseUrl)};
此外,如果您遵循 DDD 并将您的控制器放在不同的项目中,请确保您的 API 项目包含程序集参考
services.AddControllers(options =>
options.Filters.Add(new HttpResponseExceptionFilter()))
.AddApplicationPart(typeof(Startup).Assembly)
.AddApplicationPart(typeof(MyController).Assembly);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.