主要涉及到中文的位置:
String body = "谢彬测试微信支付0.01_2";
String detail = "谢彬0.01_元测试开始";
String attach = "备用参数,先留着,后面会有用的";
不光有中文还有下划线,在“统一下单”的时候,一直提示:body不是utf8编码
网上各种iso8859-1的转换说明,我只想说,每个人的代码都不一样,不能拿来直接用!!!!
我的貌似是另外一个地方编码出了问题 http://bbs.csdn.net/topics/391022204,以下是关键部分代码:
/**
* post请求并得到返回结果
* @param requestUrl
* @param requestMethod
* @param output
* @return
*/
public static String httpsRequest(String requestUrl, String requestMethod, String output) {
try{
URL url = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(requestMethod);
if (null != output) {
OutputStream outputStream = connection.getOutputStream();
outputStream.write(output.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
connection.disconnect();
return buffer.toString();
}catch(Exception ex){
ex.printStackTrace();
}
return "";
}
调用:
//构造xml参数
String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);
System.out.println(xmlInfo);
String wxUrl = UnifiedorderUrl;
String method = "POST";
String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();
System.out.println(weixinPost);