httpclient通過傳輸json格式參數操作頁面。


 
         
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);                
            }
}
 
         

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM