private static void call(String body) throws IOException{
HttpURLConnection con = null;
con = (HttpURLConnection)new URL("http://127.0.0.1:8080").openConnection();
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
con.setRequestProperty("Accept", "application/json; charset=utf-8");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();
...
}
我发布到localhost只是为了嗅探它与WireShark。 的问题是,当我的身体是像含“ò”“à”“è”“ç” ......我看到请求有乐字符串以这些字符正确字符的字符串由点代替。
例如: 如果体是“!HELLO” --->请求体是“!h.llo”
只是为了测试即时执行上述方法在Java主要和我传递参数是这样的:
String pString = "{\"titlè\":\"Hèllo Wòrld!\"}";
String params = new String(pString.getBytes("UTF-8"),"UTF-8");
....
call(body);
,这就是我得到的是Wireshark:
POST/HTTP/1.1
Accept-Charset: UTF-8
Content-Type: application/json; charset=utf-8
Accept: application/json; charset=utf-8
User-Agent: Java/1.6.0_43
Host: 127.0.0.1:8080
Connection: keep-alive
Content-Length: 24
{"titl.":"H.llo W.rld!"}
任何帮助,将不胜感激。 谢谢
2013-10-18
Ve9
+0
你有没有尝试这个办法:http://stackoverflow.com/questions/15865561/send-utf-8-chars-via-httpurlconnection-failing –
+0
感谢您的建议,但结果是一样的--- > ** {“titl。”:“H.llo W.rld!”} ** –