java的HTTP请求:返回的响应为乱码,转码根本不起作用,请看下面代码。跪求高手解答! 20

publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;Buffered... public static String sendPost(String url, String param) {
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();

conn.setRequestProperty("connection", "Keep-Alive");
// conn.setRequestProperty("Content-Type", "application/x-gzip");
// conn.setRequestProperty("Content-Length", "500");
// conn.setRequestProperty("Content-Encoding", "gzip");
conn.setRequestProperty("Host", "hispaceclt1.hicloud.com:8080");
conn.setRequestProperty("User-Agent", "Android/1.0");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream());
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
}
return result;
}

public static void main(String[] args) throws Exception {
String url = "http://hispaceclt1.hicloud.com:8080/hwmarket/api/storeApi2";
String param = "cno=4000000&code=0500&hcrId=054893FFA101451E9C266357233A8CCF&maxResults=25&method=client.getTabDetail&net=1&reqPageNum=1&serviceType=0&sign=53491011Z21130000000%40B125492651E1C4D2FB56D09E85741701&ts=1400831880271&uri=secondcattags%7C25_HOTTEST%7CF0360A7EC67F511BE040A8C025C17E79&userId=B125492651E1C4D2FB56D09E85741701&nsp_key=%2BsPNWQioG%2FRHpI3j%2FpBzup01HIY%3D";
String data = sendPost(url, param);
System.out.println(data);
}
展开
 我来答 举报
302652034_bai
推荐于2017-05-23 · TA获得超过4145个赞
知道大有可为答主
回答量:2206
采纳率:66%
帮助的人:1544万
展开全部
// 定义BufferedReader输入流来读取URL响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),“UTF-8”));
追问
不行的等哦 亲
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式