X5JsonHttpClient.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.uas.eis.task.httpclient;
  2. import com.alibaba.fastjson.JSON;
  3. import com.uas.eis.beans.x5domain.*;
  4. import com.uas.eis.dao.BaseDao;
  5. import com.uas.eis.utils.X5StringUtils;
  6. import org.apache.commons.codec.digest.DigestUtils;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.StatusLine;
  9. import org.apache.http.client.HttpResponseException;
  10. import org.apache.http.client.fluent.Form;
  11. import org.apache.http.client.fluent.Request;
  12. import org.apache.http.client.fluent.Response;
  13. import org.apache.http.util.EntityUtils;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import javax.xml.bind.DatatypeConverter;
  17. import java.io.UnsupportedEncodingException;
  18. import java.util.Date;
  19. public class X5JsonHttpClient extends X5HttpClient {
  20. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  21. /**
  22. * 构造器
  23. */
  24. public X5JsonHttpClient(String appId, String appKey, String url,BaseDao baseDao) {
  25. this.appid = appId;
  26. this.appkey = appKey;
  27. this.url = url;
  28. this.baseDao = baseDao;
  29. }
  30. public <T> X5Response<T> post(Object body, String type) {
  31. String kind="";
  32. if("ProductStock".equals(type)){
  33. kind="物料库存同步";
  34. }else if("ProductWIP".equals(type)){
  35. kind="物料WIP同步";
  36. }
  37. X5Response<T> x5Response;
  38. if (X5StringUtils.isEmpty(appid) || X5StringUtils.isEmpty(appkey) || X5StringUtils.isEmpty(url)) {
  39. throw new X5Exception("1016", "The request at least contains appid appkey and url.");
  40. }
  41. System.out.println("appid哈哈:"+appid+":"+appkey+":"+url);
  42. String bodyString = null;
  43. if (body instanceof String) {
  44. bodyString = body.toString();
  45. } else {
  46. bodyString = JSON.toJSONString(body);
  47. }
  48. String sign = upperCase(DigestUtils.md5Hex(appid + bodyString + appkey));
  49. System.out.println("3啊"+sign);
  50. //拼装header信息
  51. X5RequestHeader header = new X5RequestHeader();
  52. header.setAppid(appid);
  53. header.setSign(sign);
  54. header.setUrl(url);
  55. //拼装X5请求信息
  56. X5Request x5Request = new X5Request();
  57. x5Request.setHeader(header);
  58. x5Request.setBody(bodyString);
  59. //构造请求参数
  60. String data = X5StringUtils.encodeBase64(JSON.toJSONString(x5Request));
  61. System.out.println("4啊"+data);
  62. String encoding = null;
  63. try {
  64. encoding = DatatypeConverter.printBase64Binary("RFCMESUPSTAR:dhH9$qFr".getBytes("UTF-8")); //username password 自行修改 中间":"不可少
  65. } catch (UnsupportedEncodingException e) {
  66. e.printStackTrace();
  67. }
  68. System.out.println("5啊"+encoding);
  69. try {
  70. Response executeResult = Request.Post(url).addHeader("Content-Type", "application/x-www-form-urlencoded").setHeader("Authorization","Basic " + encoding).connectTimeout(DEFAULT_COMMECT_TIMEOUT).socketTimeout(DEFAULT_SOCKET_TIMEOUT).bodyForm(Form.form().add("data", data).build()).execute();
  71. HttpResponse httpResponse = executeResult.returnResponse();
  72. System.out.println("6啊"+httpResponse);
  73. StatusLine statusLine = httpResponse.getStatusLine();
  74. int statusCode = statusLine.getStatusCode();
  75. String responseContent = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
  76. if (statusCode == 200) {
  77. x5Response = JSON.parseObject(responseContent, X5Response.class);
  78. baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
  79. "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传成功!','','"+kind+"')");
  80. logger.info("时间:"+new Date()+"状态码:"+statusCode);
  81. } else {
  82. baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
  83. "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传失败!','"+statusLine.getStatusCode() + " " + statusLine.getReasonPhrase()+"','"+kind+"')");
  84. logger.info("时间:"+new Date()+"状态码:"+statusLine.getStatusCode()+"错误:"+statusLine.getReasonPhrase());
  85. throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
  86. }
  87. } catch (Exception ex) {
  88. x5Response = new X5Response(new X5ResponseHeader("2000", ex.getMessage()));
  89. }
  90. return x5Response;
  91. }
  92. /**
  93. * 将String转为大写
  94. */
  95. private static String upperCase(String str) {
  96. return str == null ? null : str.toUpperCase();
  97. }
  98. }