1. 程式人生 > >引數 以query String格式,方法post,傳送http請求

引數 以query String格式,方法post,傳送http請求

query String格式:     http://ip:port?name=zhangsan&pwd=123456

  1. publicclass APIHttpClient {  
  2.     // 介面地址
  3.     privatestatic String apiURL = "http://ip:8080//order";  
  4.     private Log logger = LogFactory.getLog(this.getClass());  
  5.     privatestaticfinal String pattern = "yyyy-MM-dd HH:mm:ss:SSS";  
  6.     private HttpClient httpClient = 
    null;  
  7.     private HttpPost method = null;  
  8.     privatelong startTime = 0L;  
  9.     privatelong endTime = 0L;  
  10.     privateint status = 0;  
  11.     /** 
  12.      * 介面地址 
  13.      *  
  14.      * @param url 
  15.      */
  16.     public APIHttpClient(String url) {  
  17.         if (url != null) {  
  18.             this.apiURL = url;  
  19.         }  
  20.         if
     (apiURL != null) {  
  21.             httpClient = new DefaultHttpClient();  
  22.             method = new HttpPost(apiURL);  
  23.         }  
  24.     }  
  25.     /** 
  26.      * 呼叫 API 
  27.      *  
  28.      * @param parameters 
  29.      * @return 
  30.      */
  31.     public String post(String parameters) {  
  32.         String body = null;  
  33.         logger.info("parameters:"
     + parameters);  
  34.         if (method != null & parameters != null
  35.                 && !"".equals(parameters.trim())) {  
  36.             try {  
  37.                 // 建立一個NameValuePair陣列,用於儲存欲傳送的引數
  38.                 method.addHeader("Content-type","application/json; charset=utf-8");  
  39.                 method.setHeader("Accept""application/json");  
  40.                 method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8")));  
  41.                 startTime = System.currentTimeMillis();  
  42.                 HttpResponse response = httpClient.execute(method);  
  43.                 endTime = System.currentTimeMillis();  
  44.                 int statusCode = response.getStatusLine().getStatusCode();  
  45.                 logger.info("statusCode:" + statusCode);  
  46.                 logger.info("呼叫API 花費時間(單位:毫秒):" + (endTime - startTime));  
  47.                 if (statusCode != HttpStatus.SC_OK) {  
  48.                     logger.error("Method failed:" + response.getStatusLine());  
  49.                     status = 1;  
  50.                 }  
  51.                 // Read the response body
  52.                 body = EntityUtils.toString(response.getEntity());  
  53.             } catch (IOException e) {  
  54.                 // 網路錯誤
  55.                 status = 3;  
  56.             } finally {  
  57.                 logger.info("呼叫介面狀態:" + status);  
  58.             }  
  59.         }  
  60.         return body;  
  61.     }  
  62.     publicstaticvoid main(String[] args) {  
  63.         APIHttpClient ac = new APIHttpClient(apiURL);  
  64.         JsonArray arry = new JsonArray();  
  65.         JsonObject j = new JsonObject();  
  66.         j.addProperty("orderId""中文");  
  67.         j.addProperty("createTimeOrder""2015-08-11");  
  68.         arry.add(j);  
  69.         System.out.println(ac.post(arry.toString()));  
  70.     }  
  71.     /** 
  72.      * 0.成功 1.執行方法失敗 2.協議錯誤 3.網路錯誤 
  73.      *  
  74.      * @return the status 
  75.      */
  76.     publicint getStatus() {  
  77.         return status;  
  78.     }  
  79.     publicvoid setStatus(int status) {  
  80.         this.status = status;  
  81.     }  
  82.     publiclong getStartTime() {  
  83.         return startTime;  
  84.     }  
  85.     publiclong getEndTime() {  
  86.         return endTime;  
  87.     }  
  88. }