这个方法 仍然要拼接消息体
里面的参数若不用URLEncoder.encode 对其进行编码 logcat会出现
04-30 08:39:15.480: W/System.err(2635): java.io.IOException: Content-Length exceeded 这个错误。
但是只对参数进行序列化
不对这个HttpUrlConnection 对象的消息头设置编码格式 服务器接受的消息仍然是乱码的。
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); //我尝试了 独立的添加下面的 仍然是乱码
httpurlconnection.setRequestProperty("Accept-Charset", "utf-8");
httpurlconnection.setRequestProperty("contentType", "utf-8");
后来调整为
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); 乱码就好了
本文介绍了如何解决使用HttpURLConnection发送POST请求时遇到的消息乱码问题。通过正确设置Content-Type头部参数并使用URLEncoder.encode对参数进行编码,可以避免服务器接收的数据出现乱码的情况。
1176

被折叠的 条评论
为什么被折叠?



