4

I use HTTPUrlConnection to request a REST API.
I ser "Content-type" header as follows :

urlConnection.setRequestProperty("Content-type",
                        "application/x-www-form-urlencoded");

I set HTTP body as follows :

out = urlConnection.getOutputStream();
out.write(postParameters.getBytes("UTF-8"));

I don't know if I need to escape post parameters(which is a String) when I set HTTP header and body as shown above.

I just need Yes or No as answer, but would be great if the answer explains why yes or why no.

4

1 回答 1

5

由于您要发布的数据将被解释为application/x-www-form-urlencoded,因此它必须具有以下形式:

name1=value1&name2=value2&...

因此,“值”部分必须是 URL 编码的,否则它们将不会被正确解释。

使用 POSTx-www-form-urlencoded只是将 URL 的查询字符串部分从请求中移出并移入正文。

于 2013-06-24T13:20:57.447 回答