HTTP之URLEncode加密的请求数据获取及转json

//	请求数据获取
	public static String getPostString(HttpServletRequest request) {
		BufferedReader in = null;
		String parameters = "";
		try {
			in = new BufferedReader(new InputStreamReader(
					request.getInputStream(), "utf-8"));
			String ln;
			StringBuffer stringBuffer = new StringBuffer();
			while ((ln = in.readLine()) != null) {
				stringBuffer.append(ln);
				stringBuffer.append("\r\n");
			}
			parameters = stringBuffer.toString();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return parameters;
	}
}
// urlEncode加密的数据获取及转json
// 请求数据获取
	String reqBody = getPostString(request);
	HashMap<String, String> json_data = new HashMap<String, String>();
	try {
			if (reqBody != null && !reqBody.equals("")) {
				String reqBodyinfo = URLDecoder.decode(reqBody, HTTP.UTF_8);
				String[] params = reqBodyinfo.split("&");
				for (int i = 0; i < params.length; i++) {
					int splitIndex = params[i].indexOf("=");
					if (splitIndex == -1) {
						continue;
					}
					String key = params[i].substring(0, splitIndex);
					if (!json_data.containsKey(key)) {
						if (splitIndex < params[i].length()) {
							String value = params[i].substring(splitIndex + 1);
							json_data.put(key, value);
						}
					}
				}
				//读取请求数据
				int appId = Integer.parseInt(json_data.get("appId"));
		}catch(Exception e)
		{
			logger.info("请求数据获取!" + reqBodyinfo);
			e.printStackTrace();
		}
上一篇:最近遇到的问题记录:UrlEncode、UrlDecode


下一篇:urllib 使用记录