12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package org.jeecg.config;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import org.apache.commons.codec.binary.Base64;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import org.apache.http.client.config.RequestConfig.Builder;
- import javax.xml.bind.DatatypeConverter;
- import java.io.UnsupportedEncodingException;
- //U8第三方接口调用
- public class InterfaceConnUtils {
- public static JSONArray doPost(JSONArray json, String url) {
- //定义测试返回JSON
- JSONArray onejb = new JSONArray();
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost=new HttpPost("http://192.168.1.103:8087/api/U8Server/"+url);
- httpPost.setHeader("Content-Type", "application/json");
- String tokenCode = "sap:Cwa1QJrhfhtXbzSGZi94ydBPNQCPbmz7";
- //使用base64进行加密
- byte[] tokenByte = Base64.encodeBase64((tokenCode).getBytes());
- //将加密的信息转换为string
- String tokenStr = new String(tokenByte);
- //Basic
- String token = "Basic "+tokenStr;
- String encoding="";
- try {
- encoding = DatatypeConverter.printBase64Binary("ZKANB01:Kb6a5553ec90a".getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- //把认证信息发到header中
- // httpPost.setHeader("Authorization", "Basic "+token);
- httpPost.setHeader("Authorization", "Basic "+encoding);
- //result
- //JSONObject backjson=new JSONObject();
- //backjson.put("data", json);
- StringEntity entity;
- entity = new StringEntity(json.toString(),"utf-8");
- entity.setContentEncoding("utf-8");
- entity.setContentType("application/json");
- httpPost.setEntity(entity);
- Builder customReqConf = RequestConfig.custom();
- customReqConf.setConnectTimeout(50000);
- customReqConf.setSocketTimeout(50000);
- customReqConf.setConnectionRequestTimeout(60000);
- httpPost.setConfig(customReqConf.build());
- try {
- //调用接口
- HttpResponse response = httpClient.execute(httpPost);
- //返回信息
- String resultStr = EntityUtils.toString(response.getEntity(),"UTF-8");
- onejb=JSONArray.parseArray(resultStr);
- System.out.println("返回消息内容:"+onejb);
- // jf.put("head",JsonChangeUtils.testJson(mapList));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return onejb;
- }
- }
|