| | |
| | | package com.itstyle.mdm.utils; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.itstyle.mdm.entity.HttpClientResult; |
| | | import org.apache.http.HttpStatus; |
| | | import org.apache.http.NameValuePair; |
| | |
| | | } |
| | | } |
| | | |
| | | public static String getMesJson(String json){ |
| | | JSONObject mesResJson=new JSONObject(); |
| | | JSONObject jsonobj=JSONObject.parseObject(json); |
| | | JSONArray jsonArray = jsonobj.getJSONArray("records"); |
| | | JSONArray mesResDetail=new JSONArray(); |
| | | for(int i=0;i<jsonArray.size();i++){ |
| | | JSONObject rowObject = jsonArray.getJSONObject(i); |
| | | JSONObject tmp1=new JSONObject(); |
| | | tmp1.put("Tgcoriginalcode",rowObject.getString("tgctwoclasscode")); |
| | | |
| | | tmp1.put("Code",rowObject.getString("code")); |
| | | |
| | | tmp1.put("Name",rowObject.getString("name")); |
| | | |
| | | tmp1.put("Specification",rowObject.getString("specification")); |
| | | |
| | | tmp1.put("UnitName",rowObject.getString("unitName")); |
| | | |
| | | tmp1.put("TypeCode",rowObject.getString("typeCode")); |
| | | |
| | | tmp1.put("SpecificationDisplayName",rowObject.getString("specificationDisplayName")); |
| | | |
| | | tmp1.put("SupplierName",rowObject.getString("supplierName")); |
| | | mesResDetail.add(tmp1); |
| | | } |
| | | mesResJson.put("Records",mesResDetail); |
| | | return mesResJson.toJSONString(); |
| | | } |
| | | public static String getMesSoapBody(String json){ |
| | | return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + |
| | | "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" + |
| | | " <soap12:Body>\n" + |
| | | " <ReceiveTCG xmlns=\"http://www.hitachi.com/HITPHAMS\">\n" + |
| | | " <JsonStr><![CDATA["+json+"]]></JsonStr>\n" + |
| | | " </ReceiveTCG>\n" + |
| | | " </soap12:Body>\n" + |
| | | "</soap12:Envelope>"; |
| | | } |
| | | public static HttpClientResult doPostXmlJson(String url, String xmlStr) throws Exception{ |
| | | // 创建httpClient对象 |
| | | CloseableHttpClient httpClient = HttpClients.createDefault(); |
| | | |
| | | //第二步:创建httpPost对象 |
| | | HttpPost httpPost = new HttpPost(url); |
| | | RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); |
| | | httpPost.setConfig(requestConfig); |
| | | //第三步:给httpPost设置JSON格式的参数 |
| | | StringEntity requestEntity = new StringEntity(xmlStr,"utf-8"); |
| | | requestEntity.setContentEncoding("UTF-8"); |
| | | httpPost.setHeader("Content-type", "application/soap+xml; charset=utf-8"); |
| | | httpPost.setEntity(requestEntity); |
| | | |
| | | // 创建httpResponse对象 |
| | | CloseableHttpResponse httpResponse = null; |
| | | |
| | | try { |
| | | // 执行请求并获得响应结果 |
| | | return getHttpClientResult(httpResponse, httpClient, httpPost); |
| | | } finally { |
| | | // 释放资源 |
| | | release(httpResponse, httpClient); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Description: 获得响应结果 |
| | | * |