淘宝沙箱之 中文参数的解析 java版

注:获取参数时保证要保证本地后台得到的不是乱码,之后仅在提交处理参数处编码一次。
1、获得参数的servlet
HttpServletRequest request = ServletActionContext.getRequest();
request.setCharacterEncoding("UTF-8");
String param = request.getParameter("top_parameters");

2、处理提交到淘宝沙箱的方法
public static String getResult(String urlStr, String content) {
		URL url = null;
		HttpURLConnection connection = null;

		try {
			url = new URL(urlStr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.connect();

			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//			out.writeBytes(content);
			out.write(content.getBytes("utf-8")); 
//仅在这里编码一次
			out.flush();
			out.close();

			BufferedReader reader = new BufferedReader(new InputStreamReader(connection
					.getInputStream(), "utf-8"));
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			reader.close();
			return buffer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		return null;
	}

**********************惯例的格叽格叽**************************
一周没理LYS,希望他也没空理别的小姑娘

你可能感兴趣的:(java)