SimpleHttpClientDemo.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package demo;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.entity.ContentType;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.HttpClientBuilder;
  11. import org.apache.http.util.EntityUtils;
  12. import java.io.IOException;import java.lang.String;import java.lang.System;
  13. /**
  14. * Created by lxji on 2017/9/5.
  15. */
  16. public class SimpleHttpClientDemo {
  17. private static final String BASEURL = "http://api.test.56ctms.com";
  18. private static final String token = "e75fe624-eab6-4238-85f8-1cdb82d71568";
  19. public static void main(String[] args) throws IOException {
  20. HttpPost httpPost = new HttpPost(BASEURL + "/order/create_order" + "?access_token=" + token);
  21. JSONObject order = new JSONObject();
  22. order.put("orderNumber", "wmssn123123");
  23. order.put("batchNumber", "batchNumber123123");
  24. order.put("customerOrderNumber", "customerOrderNumber123123");
  25. order.put("deliveryTime", 1502880855);
  26. order.put("appointArriveTime", (int)(System.currentTimeMillis() / 1000));
  27. order.put("startAddress", "郑州");
  28. order.put("endAddress", "武汉");
  29. order.put("consignerCity", "郑州");
  30. order.put("consignerDistrict", "郑州利泉局"); // 发货方
  31. order.put("consignerPhone", "123");
  32. order.put("consignerName", "卜桂芳"); // 发货人
  33. order.put("consignerAddress", "郑州市惠济区天河路格力广场3号库负一层");
  34. order.put("consigneeCity", "郑州");
  35. order.put("consigneeName", "黄洁丽");
  36. order.put("consigneePhone", "123");
  37. order.put("consigneeAddress", "河南郑州市二七区瓯海中路");
  38. JSONArray cargoes = new JSONArray();
  39. JSONObject cargo = new JSONObject();
  40. cargo.put("name", "xxx");
  41. cargo.put("weight", 1);
  42. cargo.put("volume", 1);
  43. cargo.put("quantity", 1);
  44. cargo.put("productModel", "zzz");
  45. cargoes.add(cargo);
  46. order.put("cargoList", cargoes);
  47. JSONArray orders = new JSONArray();
  48. orders.add(order);
  49. JSONObject request = new JSONObject();
  50. request.put("orderList", orders);
  51. StringEntity stringEntity = new StringEntity(request.toJSONString(), ContentType.APPLICATION_JSON);
  52. httpPost.setEntity(stringEntity);
  53. httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
  54. HttpClient httpClient = HttpClientBuilder.create().build();
  55. HttpResponse response = httpClient.execute(httpPost);
  56. String body = EntityUtils.toString(response.getEntity());
  57. JSONObject result = JSON.parseObject(body);
  58. System.out.println(result);
  59. }
  60. }