如何编码 HTTP POST 参数(C# 客户端到 PHP 服务器)?

发布于 2024-12-12 03:20:34 字数 700 浏览 0 评论 0原文

我无法找出将 POST 参数编码到服务器调用的最佳方法。我编写了一个由 PHP 服务器提供服务的 C# 客户端。我希望参数具有很大的灵活性,因此我当前的计划是使用 JSON 进行编码的单个参数。例如:

params = {"object":"Main","function":"doecho","params":["echothis...."]}

我使用 C# WebRequest 对象和 contentType 为“application/x-www-form-urlencoded; charset=UTF-8”。数据到达服务器后,一切都会按预期运行,直到我在数据中添加非法 JSON 字符。

例如,如果我使用以下数据,那么我无法在服务器端对其进行 json_decode 。当我使用 $this->getRequest()->getParams(); (Zend_Framework) 读取它时,服务器似乎自动将 %40 转换为双引号 (")。

params = {"object":"Main","function":"doecho","params":["echothis%25%5d%22%40%3d%26...."]}

这里的最佳实践是什么?需要对数据进行 base64 编码吗?内容类型或 php 设置是否有明显的问题?

我可以完全控制客户端和服务器,所以我想知道什么是正确/最好的做法。

I'm having trouble figuring out the best way to encode the POST parameters to a server call. I writing a C# client that will be served by a PHP server. I want to allow a great deal of flexibility in the parameters, so my current plan is to have a single parameter that I use JSON to encode. For example:

params = {"object":"Main","function":"doecho","params":["echothis...."]}

I'm using the C# WebRequest object and contentType of "application/x-www-form-urlencoded; charset=UTF-8". The data gets to the server and everything works as expected until I add illegal JSON characters in the data.

For example, if I use the following data, then I can't do a json_decode on it on the server side. The server appears to automatically turn the %40 into a double-quote (") when I read it with $this->getRequest()->getParams(); (Zend_Framework).

params = {"object":"Main","function":"doecho","params":["echothis%25%5d%22%40%3d%26...."]}

What is the best practice here? Do I need to base64 encode the data? Is there something obvious I'm missing with the content type or a php setting?

I have full control over the client and server, so I'd like to know what is the right/best thing to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2024-12-19 03:20:34

虽然任何内容类型都可以用于上传到 HTTP,但在实践中使用了三种:

  1. 一种是由给定服务的文档专门设置的。
  2. application/x-www-form-urlencoded - HTML 表单默认使用的值。
  3. multipart/form-data - HTML 表单使用的其他表单,在包含表单上传时需要。

由于 2 和 3 如此常用(因为所有浏览器都支持它们来提交表单),所以几乎所有服务器端技术都有处理它们的方法。因此,除非 PHP 部分做了一些奇怪的事情,否则您应该能够使用其中任何一个。

application/x-www-form-urlencoded 不适合某些数据,但对于其用途来说是最简单的。它与为 GET 表单请求创建查询字符串的方式几乎相同,但作为 POST 内容。

因此,您希望您的内容是:

"params=" + Uri.EscapeDataString(paramData)

因此,第一个变为:

params=%7B%22object%22%3A%22Main%22%2C%22function%22%3A%22doecho%22%2C%22params%22%3A%5B%22echothis....%22%5D%7D

第二个:

params=%7B%22object%22%3A%22Ccmes_Main%22%2C%22function%22%3A%22doecho%22%2C%22params%22%3A%5B%22echothis%2525%255d%2522%2540%253d%2526....%22%5D%7D

PHP 的内置函数将返回到您问题中的形式。

While potentially any content-type can be used to upload to HTTP, there are three used in practice:

  1. One specifically set by a given service's documentation.
  2. application/x-www-form-urlencoded - the default used by HTML forms.
  3. multipart/form-data - the other form used by HTML forms, required when it includes form uploads.

Due to 2 and 3 being so commonly used (as they are supported by all browser's for submitting forms), pretty much all server-side tech has things to handle them. So unless the PHP part does something strange, you should be able to use either.

application/x-www-form-urlencoded isn't appropriate for some data, but is the simplest for what it is used for. It's pretty much the same as the way query-strings are created for GET form requests, but as POST content.

Hence you want your content to be:

"params=" + Uri.EscapeDataString(paramData)

As such the first becomes:

params=%7B%22object%22%3A%22Main%22%2C%22function%22%3A%22doecho%22%2C%22params%22%3A%5B%22echothis....%22%5D%7D

And the second:

params=%7B%22object%22%3A%22Ccmes_Main%22%2C%22function%22%3A%22doecho%22%2C%22params%22%3A%5B%22echothis%2525%255d%2522%2540%253d%2526....%22%5D%7D

Both of which PHP's built-ins will turn back into the forms in your question.

看海 2024-12-19 03:20:34

我的第一个想法是将其编码为 base64。我认为这应该是最简单的方法。

来自 arcanecode.com

static public string EncodeTo64(string toEncode)
{
  byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
  string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
  return returnValue;
}

My first thought was encoding it as base64. I think this should be the simplies way.

From arcanecode.com:

static public string EncodeTo64(string toEncode)
{
  byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
  string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
  return returnValue;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文