package demo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.io.IOException;import java.lang.String;import java.lang.System; /** * Created by lxji on 2017/9/5. */ public class SimpleHttpClientDemo { private static final String BASEURL = "http://api.test.56ctms.com"; private static final String token = "e75fe624-eab6-4238-85f8-1cdb82d71568"; public static void main(String[] args) throws IOException { HttpPost httpPost = new HttpPost(BASEURL + "/order/create_order" + "?access_token=" + token); JSONObject order = new JSONObject(); order.put("orderNumber", "wmssn123123"); order.put("batchNumber", "batchNumber123123"); order.put("customerOrderNumber", "customerOrderNumber123123"); order.put("deliveryTime", 1502880855); order.put("appointArriveTime", (int)(System.currentTimeMillis() / 1000)); order.put("startAddress", "郑州"); order.put("endAddress", "武汉"); order.put("consignerCity", "郑州"); order.put("consignerDistrict", "郑州利泉局"); // 发货方 order.put("consignerPhone", "123"); order.put("consignerName", "卜桂芳"); // 发货人 order.put("consignerAddress", "郑州市惠济区天河路格力广场3号库负一层"); order.put("consigneeCity", "郑州"); order.put("consigneeName", "黄洁丽"); order.put("consigneePhone", "123"); order.put("consigneeAddress", "河南郑州市二七区瓯海中路"); JSONArray cargoes = new JSONArray(); JSONObject cargo = new JSONObject(); cargo.put("name", "xxx"); cargo.put("weight", 1); cargo.put("volume", 1); cargo.put("quantity", 1); cargo.put("productModel", "zzz"); cargoes.add(cargo); order.put("cargoList", cargoes); JSONArray orders = new JSONArray(); orders.add(order); JSONObject request = new JSONObject(); request.put("orderList", orders); StringEntity stringEntity = new StringEntity(request.toJSONString(), ContentType.APPLICATION_JSON); httpPost.setEntity(stringEntity); httpPost.setHeader("Content-Type", "application/json; charset=utf-8"); HttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse response = httpClient.execute(httpPost); String body = EntityUtils.toString(response.getEntity()); JSONObject result = JSON.parseObject(body); System.out.println(result); } }