C# WebClient 使用 UploadString 从同样在 C# 中的 ApiController 调用 HttpPost 方法。 415 或 400 错误
C# WebClient using UploadString to call HttpPost method from an ApiController also in C#. 415 or 400 error
我希望在 C# 中也能在 C# 中调用 ApiController,但在使用 WebClient 实例的 UploadString 方法上传 Json 时出现错误 415 或 400。
服务器代码是自动生成的调用TestController。该文件正是 Visual Studio 2019 生成它的方式。
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
// GET: api/Test
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// POST: api/Test
[HttpPost]
public void Post([FromBody] string value)
{
}
...
}
客户端代码如下所示:
WebClient client = new WebClient();
client.UploadString("https://localhost:44345/api/Test", "ABC"); // Edit: "ABC" is not a valid JSON
我正在
System.Net.WebException: 'The remote server returned an error: (415) Unsupported Media Type.'
所以在谷歌搜索之后,大多数建议是 ContentType 没有得到指定,如果我添加
client.Headers[HttpRequestHeader.ContentType] = "application/json";
我得到 System.Net.WebException:'The remote server returned an error: (400) Bad Request.'
有线索吗?
似乎问题与 POST
/PUT
/PATCH
有关...如果我执行 GET
,它会工作并返回样本给我数据 ["value1","value2"]
编辑:我不坚持使用 WebClient.UploadString 方法,但我想要一个不涉及 25 行自定义代码的解决方案...我的意思是我无法相信它有那么难您可以在 jQuery 中使用一行来完成。
I'm getting
System.Net.WebException: 'The remote server returned an error: (415) Unsupported Media Type.'
当您使用 [FromBody]
时,Content-Type
header 用于确定如何解析请求 body。当未指定 Content-Type
时,model-binding 进程不知道如何使用 body,因此 returns 415。
I get System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'
通过将 Content-Type
header 设置为 application/json
,您指示 model-binding 进程将数据视为 JSON,但是 ABC
本身无效 JSON。如果你只想发送一个 JSON-encoded 字符串,你也可以将值用引号引起来,像这样:
client.UploadString("https://localhost:44345/api/Test", "\"ABC\"");
"ABC"
是有效的 JSON 字符串,将被您的 ASP.NET 核心 API.
接受
简单的解决方案:
在调用 API、
时在 header 中指定 Content-type
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "text/json");
client.UploadString("https://localhost:44345/api/Test", "\"ABC\"");
编辑:
不要使用 [From_Body]
属性,因为它具有糟糕的错误处理能力,
参见 Here。
如果请求 body 有任何无效输入(语法错误、不受支持的输入),那么它将针对错误请求和不受支持的内容抛出 400
和 415
。出于同样的原因,它可能会将 null 作为请求的输入 body 它不理解格式。
因此,删除属性并尝试以纯格式上传字符串,因为它只接受字符串,并且您在发出请求时不需要指定 Content-Type 属性。
[HttpPost]
public void Post(string value)
{
}
并像您在原始 post 中那样称呼它。
WebClient client = new WebClient();
client.UploadString("https://localhost:44345/api/Test", "ABC");
我希望在 C# 中也能在 C# 中调用 ApiController,但在使用 WebClient 实例的 UploadString 方法上传 Json 时出现错误 415 或 400。
服务器代码是自动生成的调用TestController。该文件正是 Visual Studio 2019 生成它的方式。
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
// GET: api/Test
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// POST: api/Test
[HttpPost]
public void Post([FromBody] string value)
{
}
...
}
客户端代码如下所示:
WebClient client = new WebClient();
client.UploadString("https://localhost:44345/api/Test", "ABC"); // Edit: "ABC" is not a valid JSON
我正在 System.Net.WebException: 'The remote server returned an error: (415) Unsupported Media Type.'
所以在谷歌搜索之后,大多数建议是 ContentType 没有得到指定,如果我添加
client.Headers[HttpRequestHeader.ContentType] = "application/json";
我得到 System.Net.WebException:'The remote server returned an error: (400) Bad Request.'
有线索吗?
似乎问题与 POST
/PUT
/PATCH
有关...如果我执行 GET
,它会工作并返回样本给我数据 ["value1","value2"]
编辑:我不坚持使用 WebClient.UploadString 方法,但我想要一个不涉及 25 行自定义代码的解决方案...我的意思是我无法相信它有那么难您可以在 jQuery 中使用一行来完成。
I'm getting System.Net.WebException: 'The remote server returned an error: (415) Unsupported Media Type.'
当您使用 [FromBody]
时,Content-Type
header 用于确定如何解析请求 body。当未指定 Content-Type
时,model-binding 进程不知道如何使用 body,因此 returns 415。
I get System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'
通过将 Content-Type
header 设置为 application/json
,您指示 model-binding 进程将数据视为 JSON,但是 ABC
本身无效 JSON。如果你只想发送一个 JSON-encoded 字符串,你也可以将值用引号引起来,像这样:
client.UploadString("https://localhost:44345/api/Test", "\"ABC\"");
"ABC"
是有效的 JSON 字符串,将被您的 ASP.NET 核心 API.
简单的解决方案:
在调用 API、
时在 header 中指定Content-type
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "text/json");
client.UploadString("https://localhost:44345/api/Test", "\"ABC\"");
编辑:
不要使用 [From_Body]
属性,因为它具有糟糕的错误处理能力,
参见 Here。
如果请求 body 有任何无效输入(语法错误、不受支持的输入),那么它将针对错误请求和不受支持的内容抛出 400
和 415
。出于同样的原因,它可能会将 null 作为请求的输入 body 它不理解格式。
因此,删除属性并尝试以纯格式上传字符串,因为它只接受字符串,并且您在发出请求时不需要指定 Content-Type 属性。
[HttpPost]
public void Post(string value)
{
}
并像您在原始 post 中那样称呼它。
WebClient client = new WebClient();
client.UploadString("https://localhost:44345/api/Test", "ABC");