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);
} 展开
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);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询