java项目之间的通信

    技术2023-07-12  105

    基于apache httpClient4.5的post请求 1.发送请求方

    package com.fh.util; import java.io.IOException; import java.net.SocketTimeoutException; import java.util.List; import net.sf.json.JSONObject; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpRequest2{ static HttpRequest2 HttpRequest=new HttpRequest2(); public static Logger logger = Logger.getLogger(HttpRequest.getClass()); /** * post请求(apache httpClient4.5) * * @param method:请求方法 * @param date:请求数据 */ public static JSONObject doPost(String method, List<JSONObject> date) { CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse res = null; String url = "http://192.168.100.20:8089/E04092/" + method; HttpPost post = new HttpPost(url); JSONObject response = JSONObject.fromObject("{"+'"'+"msg"+'"'+':'+'"'+"error"+'"'+"}");; try { StringEntity s = new StringEntity(date.toString(),"UTF-8"); //s.setContentEncoding("UTF-8"); s.setContentType("application/json"); post.setEntity(s); post.addHeader("content-type", "text/xml"); RequestConfig config=RequestConfig.custom() .setConnectTimeout(8000) // 连接主机服务超时时间 .setConnectionRequestTimeout(8000) // 请求超时时间 .setSocketTimeout(30000) // 数据读取超时时间 .build(); post.setConfig(config); res = client.execute(post); // client对象执行post请求,并返回响应参数对象 // System.out.println("res.getStatusLine().getStatusCode():"+res.getStatusLine().getStatusCode()); // System.out.println("HttpStatus.SC_OK:"+HttpStatus.SC_OK); if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = res.getEntity(); String result = EntityUtils.toString(entity,"UTF-8"); // 同一个httpclient中只能有一个获取entity的方法,不然会出错 response = JSONObject.fromObject(result); } } catch (ConnectTimeoutException e) { e.printStackTrace(); logger.info("用户请求连接超时"); System.out.println("请求连接超时:" + e.getMessage()); } catch (SocketTimeoutException e) { e.printStackTrace(); logger.info("用户ID为"+date.get(0).getString("JS_USER")+"等待服务器响应超时,提交给服务器处理数据为"+date.get(0).getString("SP_CPRICE")+"元产品"+date.size()+"件"); System.out.println("服务器响应超时:" + e.getMessage()); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); }finally { // 关闭资源 if (null != res) { try { res.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != client) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } return response; } }

    2.处理请求方

    /** * 接收传递的json参数解析(寄售商品的信息) * * @param request * @param response * @return * @data:日期:2016年9月9日10:50:52 */ @ResponseBody @RequestMapping(value="getJson",produces = "application/json;charset=UTF-8") public String getJson(HttpServletRequest request,HttpServletResponse response) { StringBuffer json = new StringBuffer(); String line = null; String info="{"+'"'+"msg"+'"'+':'+'"'+"error"+'"'+"}"; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { System.out.println("line:" + line); json.append(line); } } catch (Exception e) { e.printStackTrace(); } System.out.println("接收参数完毕:"+json); if(!json.toString().equals("") && json.toString()!=null){ try { info=auctionService.save_Warehousing(json.toString()); } catch (Exception e) { e.printStackTrace(); } } return info; } /** * 寄售商品入库 * * @param * @throws Exception */ public String save_Warehousing(String info) throws Exception { String str = ""; JSONArray getJsonArray=JSONArray.fromObject(info);//将结果转换成JSONArray对象的形式 JSONObject getJsonObj=new JSONObject(); //创建josn对象 for(int i=0;i<getJsonArray.size();i++){ //循环遍历josn数组 getJsonObj=getJsonArray.getJSONObject(i); int SP_TYPE=getJsonObj.getInt("SP_TYPE"); System.out.println("寄售商品类型:"+SP_TYPE); if(SP_TYPE==1){ //寄售 的是普通商品 str=Eighteen(getJsonObj); }else if(SP_TYPE==2){ //寄售 的是499商品 str=save_ninety_nine(getJsonObj); } } return str; }
    Processed: 0.008, SQL: 9