public class DGMApiTest {
     public static void main(String[] args) throws HttpException, IOException  {
         String url="https://api.dhlecommerce.dhl.com/rest/v2/Tracking";      //一个物流网站示例  
                getHomePage(url);
                
            }
                       
            public static void getHomePage(String url) throws HttpException, IOException {
                DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager());
                HttpPost postMethod = new HttpPost(url);
                String json = "{ \"trackItemRequest\": {\"token\": \"32e657a041df15a1bcccffff8cbf61c7\",\"messageLanguage\": \"en\",\"messageVersion\": \"1.1\",\"trackingReferenceNumber\": [\"LW043144320DE\"] } }";
                System.out.println(json);
                postMethod.setHeader("Content-Type", "application/json; charset=utf-8");        
                StringEntity se = new StringEntity(json);
                se.setContentType("text/json");
                postMethod.setEntity(se);
                CloseableHttpResponse response = null;
                response = client.execute(postMethod);
                HttpEntity entity = null;
                entity = response.getEntity();
                String track = EntityUtils.toString(entity, "UTF-8");
                System.out.println(track);                
            }
}

 

 

 

posted on 2018-01-24 10:18  艾缇小王子  阅读(1374)  评论(0编辑  收藏  举报