1. 程式人生 > >微信支付:body不是utf8編碼

微信支付:body不是utf8編碼

主要涉及到中文的位置:

String body = "謝彬測試微信支付0.01_2";
String detail = "謝彬0.01_元測試開始";
String attach = "備用引數,先留著,後面會有用的";


不光有中文還有下劃線,在“統一下單”的時候,一直提示:body不是utf8編碼

網上各種iso8859-1的轉換說明,我只想說,每個人的程式碼都不一樣,不能拿來直接用!!!!

/**
	 * 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);