您的位置 首页 编程知识

将 PHP POST 请求转换为 C# 实现

本文旨在帮助开发者将 PHP 中接收 application/x-www-form-urlencoded 数据…

将 PHP POST 请求转换为 C# 实现

本文旨在帮助开发者将 PHP 中接收 application/x-www-form-urlencoded 数据的 POST 请求转换为 C# .NET Core 中的等效实现。我们将探讨如何正确设置 Content-Type 头部,以及如何在 C# 中接收和处理来自第三方 API 的数据,从而避免 415 Unsupported Media Type 错误。

在 C# .NET Core 中,当与期望 application/x-www-form-urlencoded 数据的 API 交互时,需要确保正确设置请求的 Content-Type 头部,并正确处理接收到的数据。以下是如何解决 415 Unsupported Media Type 错误的详细步骤和示例代码。

1. 理解问题:Content-Type 头部的重要性

415 Unsupported Media Type 错误表明服务器无法理解客户端发送的数据格式。在你的例子中,第三方 API 期望接收 application/x-www-form-urlencoded 格式的数据,这通常是 HTML 时使用的格式。 如果你的 C# 代码没有正确设置 Content-Type 头部,或者发送了其他格式的数据(例如 JSON),服务器就会返回此错误。

立即学习“”;

2. C# 代码实现:接收 application/x-www-form-urlencoded 数据

首先,你需要修改你的 C# 控制器方法,以正确接收和处理来自 application/x-www-form-urlencoded 数据的请求。可以使用 [FromForm] 属性来绑定 POST 请求中的表单数据。

using Microsoft.AspNetCore.Mvc; using System.Net; using System.Threading.Tasks;  namespace YourProject.Controllers {     [ApiController]     [Route("[controller]")]     public class YourController : ControllerBase     {         [HttpPost("b_notice")]         [ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]         public async Task<ActionResult> APIUrl([FromForm] string mm_id, [FromForm] string oo_id)         {             try             {                 // 在这里处理接收到的 mm_id 和 oo_id 数据                 string result = $"mm_id: {mm_id}, oo_id: {oo_id}";                 return Ok(result);             }             catch (Exception ex)             {                 return BadRequest(ex.Message);             }         }     } }
登录后复制

解释:

  • [ApiController] 和 [Route(“[controller]”)] 是 ASP.NET Core Web API 的标准设置。
  • [HttpPost(“b_notice”)] 指定该方法处理 HTTP POST 请求,并且路由为 /YourController/b_notice (假设控制器名为 YourController)。
  • [FromForm] 属性告诉 ASP.NET Core 从请求的表单数据中绑定 mm_id 和 oo_id 参数。 这对于处理 application/x-www-form-urlencoded 格式的数据至关重要。
  • ProducesResponseType 属性用于指定 API 返回的类型和 HTTP 状态码。

3. 客户端请求:设置 Content-Type 头部

确保你的客户端(例如,使用 HttpClient)在发送 POST 请求时设置了 Content-Type 头部为 application/x-www-form-urlencoded。

using System.Net.Http; using System.Text; using System.Threading.Tasks;  public class ApiClient {     public static async Task<string> PostData(string url, string mm_id, string oo_id)     {         using (var client = new HttpClient())         {             var content = new FormUrlEncodedContent(new[]             {                 new KeyValuePair<string, string>("mm_id", mm_id),                 new KeyValuePair<string, string>("oo_id", oo_id)             });              HttpResponseMessage response = await client.PostAsync(url, content);              if (response.IsSuccessStatusCode)             {                 return await response.Content.ReadAsStringAsync();             }             else             {                 return $"Error: {response.StatusCode}";             }         }     } }
登录后复制

解释:

  • HttpClient 用于发送 HTTP 请求。
  • FormUrlEncodedContent 类用于创建 application/x-www-form-urlencoded 格式的请求体。 它接受一个 KeyValuePr 的集合,其中键是表单字段的名称,值是表单字段的值。
  • client.PostAsync(url, content) 发送 POST 请求到指定的 URL,并将 content 作为请求体发送。

使用示例:

string result = await ApiClient.PostData("https://your-api-endpoint/YourController/b_notice", "123", "456"); Console.WriteLine(result);
登录后复制

4. 使用 Postman 进行测试

Postman 是一个非常有用的,可以用来测试你的 API。 在 Postman 中,你需要设置以下内容:

  • 请求类型: POST
  • URL: 你的 API 端点 (例如: https://your-api-endpoint/YourController/b_notice)
  • Headers: 设置 Content-Type 为 application/x-www-form-urlencoded
  • Body: 选择 x-www-form-urlencoded 选项,并添加 mm_id 和 oo_id 。

5. 注意事项

  • 数据验证: 始终对接收到的数据进行验证,以确保其符合预期的格式和范围。
  • 异常处理: 在 try-catch 块中捕获异常,并提供有意义的错误消息。
  • 安全性: 对于,使用 HTTPS 协议进行加密传输。
  • 第三方库: 如果需要更高级的功能,可以考虑使用第三方库来处理 HTTP 请求和响应。

总结

通过正确设置 Content-Type 头部,并使用 [FromForm] 属性来绑定表单数据,你可以在 C# .NET Core 中成功接收和处理 application/x-www-form-urlencoded 格式的 POST 请求。 使用 Postman 等工具进行测试,可以帮助你验证你的 API 是否正常工作。 确保进行适当的数据验证和异常处理,以提高应用程序的健壮性和安全性。

以上就是将 PHP POST 请求转换为 C# 实现的详细内容,更多请关注php中文网其它相关文章!

本文来自网络,不代表四平甲倪网络网站制作专家立场,转载请注明出处:http://www.elephantgpt.cn/14008.html

作者: nijia

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

18844404989

在线咨询: QQ交谈

邮箱: 641522856@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部