java后台http请求

maven json 引入:

        
        
			net.sf.json-lib
			json-lib
			2.4
			jdk15
	
        
        
		    com.fasterxml.jackson.core
		    jackson-databind
		    2.8.1
	

 

后台:

        @ResponseBody
	@RequestMapping("XXX.do")
	public JSONObject XXX(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String name = request.getParameter("name");
		String model = request.getParameter("model");
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(GetScriptProperties.getValue("XX"));
		String result = null;
		
		
		JSONObject  obj = new JSONObject();
		obj.put("name", name);
		obj.put("mode", model);
		logger.info("inputJson:"+obj.toString());
		// 解决中文乱码问题
                StringEntity stringEntity = new StringEntity(obj.toString(), "UTF-8");
                stringEntity.setContentEncoding("UTF-8");
                httpPost.setEntity(stringEntity);

                //执行post请求
		HttpResponse httpResponse;
		try {
		    httpResponse = client.execute(httpPost);
	            if(response != null){  
	            HttpEntity resEntity = httpResponse.getEntity();  
	                if(resEntity != null){  
	                    result = EntityUtils.toString(resEntity,"utf-8");  
	                }  
	            }
        
		} catch (Exception e) {
			logger.info("Exception:"+e.getMessage());
			JSONObject jsonResult = JSONObject.fromObject("{resultCode: '1', resultDesc: '执行失败:"+e.getMessage()+"'}");
			
			return jsonResult;
		}
		
                logger.info("resultInfo:"+result);
                JSONObject jsonResult = JSONObject.fromObject(result);
        
		return jsonResult;
	}

 

你可能感兴趣的:(javaWeb,http,java)