如何编码 HTTP POST 参数(C# 客户端到 PHP 服务器)?
我无法找出将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然任何内容类型都可以用于上传到 HTTP,但在实践中使用了三种:
由于 2 和 3 如此常用(因为所有浏览器都支持它们来提交表单),所以几乎所有服务器端技术都有处理它们的方法。因此,除非 PHP 部分做了一些奇怪的事情,否则您应该能够使用其中任何一个。
application/x-www-form-urlencoded 不适合某些数据,但对于其用途来说是最简单的。它与为 GET 表单请求创建查询字符串的方式几乎相同,但作为 POST 内容。
因此,您希望您的内容是:
因此,第一个变为:
第二个:
PHP 的内置函数将返回到您问题中的形式。
While potentially any content-type can be used to upload to HTTP, there are three used in practice:
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:
As such the first becomes:
And the second:
Both of which PHP's built-ins will turn back into the forms in your question.
我的第一个想法是将其编码为 base64。我认为这应该是最简单的方法。
来自 arcanecode.com:
My first thought was encoding it as base64. I think this should be the simplies way.
From arcanecode.com: