InterfaceConnUtils.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package org.jeecg.config;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.apache.commons.codec.binary.Base64;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.config.RequestConfig;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.util.EntityUtils;
  12. import org.apache.http.client.config.RequestConfig.Builder;
  13. import javax.xml.bind.DatatypeConverter;
  14. import java.io.UnsupportedEncodingException;
  15. //U8第三方接口调用
  16. public class InterfaceConnUtils {
  17. public static JSONArray doPost(JSONArray json, String url) {
  18. //定义测试返回JSON
  19. JSONArray onejb = new JSONArray();
  20. HttpClient httpClient = new DefaultHttpClient();
  21. HttpPost httpPost=new HttpPost("http://192.168.1.103:8087/api/U8Server/"+url);
  22. httpPost.setHeader("Content-Type", "application/json");
  23. String tokenCode = "sap:Cwa1QJrhfhtXbzSGZi94ydBPNQCPbmz7";
  24. //使用base64进行加密
  25. byte[] tokenByte = Base64.encodeBase64((tokenCode).getBytes());
  26. //将加密的信息转换为string
  27. String tokenStr = new String(tokenByte);
  28. //Basic
  29. String token = "Basic "+tokenStr;
  30. String encoding="";
  31. try {
  32. encoding = DatatypeConverter.printBase64Binary("ZKANB01:Kb6a5553ec90a".getBytes("UTF-8"));
  33. } catch (UnsupportedEncodingException e) {
  34. e.printStackTrace();
  35. }
  36. //把认证信息发到header中
  37. // httpPost.setHeader("Authorization", "Basic "+token);
  38. httpPost.setHeader("Authorization", "Basic "+encoding);
  39. //result
  40. //JSONObject backjson=new JSONObject();
  41. //backjson.put("data", json);
  42. StringEntity entity;
  43. entity = new StringEntity(json.toString(),"utf-8");
  44. entity.setContentEncoding("utf-8");
  45. entity.setContentType("application/json");
  46. httpPost.setEntity(entity);
  47. Builder customReqConf = RequestConfig.custom();
  48. customReqConf.setConnectTimeout(50000);
  49. customReqConf.setSocketTimeout(50000);
  50. customReqConf.setConnectionRequestTimeout(60000);
  51. httpPost.setConfig(customReqConf.build());
  52. try {
  53. //调用接口
  54. HttpResponse response = httpClient.execute(httpPost);
  55. //返回信息
  56. String resultStr = EntityUtils.toString(response.getEntity(),"UTF-8");
  57. onejb=JSONArray.parseArray(resultStr);
  58. System.out.println("返回消息内容:"+onejb);
  59. // jf.put("head",JsonChangeUtils.testJson(mapList));
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. return onejb;
  64. }
  65. }