在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?

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

问题描述

我正在使用 Apache HttpClient 发送RESTful JSON POST请求(到第三方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天全站免登陆