我有兩個名為app.account和的 ASP.NET Core Web API 專案core.account。我這兩個專案都有一些像這樣的測驗 API 端點。
在app.account:
namespace app.account.Controllers
{
[ApiController]
public class SubInteractiveStatementController : ControllerBase
{
[HttpPost]
[Route("account/istatement")]
public async Task<ActionResult> Test(Test cif)
{
return Ok("hello gamma");
}
}
}
public class Test
{
public string cif { get; set; }
}
在core.account:
namespace core.account.Controllers
{
[ApiController]
public class CasaDetailController : ControllerBase
{
[HttpPost]
[Route("account/istatement")]
public async Task<ActionResult> Test(Test cif)
{
return Ok("hello gamma");
}
}
}
我在整個客戶端呼叫上述 API 端點,如下所示:
try {
CommonResponse commonResponse = new();
var data_ = JsonConvert.SerializeObject(test);
var buffer_ = System.Text.Encoding.UTF8.GetBytes(data_);
var byteContent_ = new ByteArrayContent(buffer_);
byteContent_.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//string _urls = "http://localhost:5088/account/istatement";
string _urls = "http://localhost:5035/account/istatement";
var responses_ = await _httpClient.PostAsync(_urls, byteContent_);
if (responses_.StatusCode == HttpStatusCode.OK) {
Console.WriteLine("[GetPrimeryAccount] Response: Success");
string body = await responses_.Content.ReadAsStringAsync();
var rtns = JsonConvert.DeserializeObject < CommonResponse > (body);
}
} catch (global::System.Exception) {
throw;
}
http://localhost:5088/account/istatement 是 app.account API 端點,http://localhost:5035/account/istatement 是 core.account API 端點。
當我呼叫 時app.account,我收到以下錯誤。
{ StatusCode:400,ReasonPhrase:'Bad Request',版本:1.1,內容:System.Net.Http.HttpConnectionResponseContent,標頭:{ 日期:星期三,2022 年 10 月 19 日 08:43:21 GMT 服務器:Kestrel 傳輸編碼:分塊}}
但它正確地呼叫了core.accountAPI 端點。
兩個 URL 都是正確的,我使用 Postman 檢查了兩個端點,并且作業正常。
這可能是什么原因?會不會是 HTTP 版本問題?我怎樣才能確定這個的確切原因?請幫忙。謝謝
uj5u.com熱心網友回復:
關于請求

uj5u.com熱心網友回復:
問題似乎在于您沒有與我們分享的部分。請在此處分享這兩個 API 專案startup.cs。提示,如果您使用任何自定義標頭驗證中間件,請確保正確傳遞這些標頭值。否則,可能會導致發生 400 Bad Request。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiddleware<YourcCustomHeaderValidationClass>(); // Custom header validation
app.UseRouting();
}
比較這兩個startp.cs檔案,您將能夠確定那里的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/518145.html
標籤:C#网asp.net 核心httpclient错误的请求
