在 Java 中,在发送 JSON POST 时使用 HttpPost (apache commons HttpClient) - 我应该对正文进行 URL 编码吗? [英] In Java, using HttpPost (apache commons HttpClient) when sending JSON POST - should I URL encode the body?

查看:28
本文介绍了在 Java 中,在发送 JSON POST 时使用 HttpPost (apache commons HttpClient) - 我应该对正文进行 URL 编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache HttpClient 发送 RESTful JSON POST 请求(到第 3 方 API)

I'm sending a RESTful JSON POST request using Apache HttpClient (to a 3rd party API)

  • 我应该对 JSON 正文进行 URL 编码吗?

  • Should I URL encode the JSON body?

如果内容中的某些内容已经是 URL 编码的(例如,我发送的 HTML 具有一些带有 URL 编码字符的链接,例如@22),我是否应该期望在另一侧获得内容而不是解码了吗?

And if something in the content is already URL encoded (e.g. I send HTML that has some links with URL encoded chars, e.g. @22) should I expect to get the content as is on the other side without it being decoded?

例如如果我正在做这样的事情

E.g. if i'm doing something like this

String html = "<a href='http://example.com?charOfTheDay=%22'>click me</a>";
// Build the JSON object 

JSONObject jsonObj = new JSONObject();
jsonObj.put("html", html);
jsonObj.put("otherKey",otherValue);
//...

// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);

在获得html"键的值后,我是否应该期望在接收端获得相同的值?

Should I expect getting the same value on the receiving end, after getting the value of the "html" key?

例如在接收端

//after parsing the request string to a JSON object
String html = inputJsonObject.get("html") 
// should return "<a href='http://example.com?charOfTheDay=%22'>click me</a>"

我是否还需要采取任何其他步骤来确保我发送的内容是按原样"收到的内容?

Are there any other steps I need to do to make sure what I send is what is received "as is"?

推荐答案

这里有两种情况你需要担心:

There's sort of two contexts you have to worry about here:

  1. JSON
    您必须确保生成的 JSON 是有效的 JSON (duh).这意味着确保所有 { }[ ] 语法都在正确的位置,并确保您插入到 JSON 对象中的字段值被安全转义(就像在您的问题中转义该 HTML 片段一样 - 一些引号字符需要转义).但是因为您使用的是标准的 JSON Java 库,所以您不必担心这个...它会为您处理所有这些.

  1. JSON
    You have to make sure that the JSON you generate is valid JSON (duh). This means making sure that all the { } and [ ] syntax is in the right place and making sure that the field values you are inserting into the JSON object are safely escaped (like escaping that HTML snippet in your question --some of the quote characters would need to be escaped). BUT because you're using a standard JSON Java library, you don't have to worry about this...it will take care of all this for you.

HTTP
然后必须将 JSON 字符串插入到 HTTP 请求正文中.这里不需要进行转义——只需插入原始 JSON 字符串.HTTP 作为一种协议,将接受请求/响应正文中的任何内容,包括原始二进制数据.

HTTP
The JSON string then has to be inserted into the HTTP request body. No escaping needs to be done here--just insert the raw JSON string. HTTP, as a protocol, will accept anything inside the request/response bodies, including raw binary data.

无论如何,这有点长,但我希望它有所帮助.

Anyway that was kind of long, but I hope it helped.

这篇关于在 Java 中,在发送 JSON POST 时使用 HttpPost (apache commons HttpClient) - 我应该对正文进行 URL 编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆