程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java應用httpclient發送post要求示例

java應用httpclient發送post要求示例

編輯:關於JAVA

java應用httpclient發送post要求示例。本站提示廣大學習愛好者:(java應用httpclient發送post要求示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java應用httpclient發送post要求示例正文



package org.ssi.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


public class APIHttpClient {

 //接口地址
 private String apiURL = "";
 private Log logger = LogFactory.getLog(this.getClass());
 private static final String pattern = "yyyy-MM-dd HH:mm:ss:SSS";
        private HttpClient httpClient = null;
 private HttpPost method = null;
 private long startTime = 0L;
 private long endTime = 0L;
 private int status = 0;
 /**
  * 接口地址
  * @param url
  */
 public APIHttpClient(String url){

  if(url != null)
  {
   this.apiURL = url;
  }
  if(apiURL != null)
  {
                    httpClient = new DefaultHttpClient();
                    method = new HttpPost(apiURL);

  }
 }

 /**
  * 挪用 API
  * @param parameters
  * @return
  */
 public String post(String parameters)
 {
  String body = null;
                logger.info("parameters:" + parameters);

               
  if(method != null & parameters != null  && !"".equals(parameters.trim()))
  {
                    JSONArray jsonObject = JSONArray.fromObject(parameters);
                    logger.info("json:" + jsonObject.toString());
                    try{

                        List<NameValuePair> params=new ArrayList<NameValuePair>(); 
                        //樹立一個NameValuePair數組,用於存儲欲傳送的參數 
                        params.add(new BasicNameValuePair("data",parameters)); 
                        //添加參數 
                        method.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); 

                        startTime = System.currentTimeMillis();

                        //設置編碼 
                        HttpResponse response=httpClient.execute(method); 
                        endTime = System.currentTimeMillis();
                        int statusCode = response.getStatusLine().getStatusCode();
                        logger.info("statusCode:" + statusCode);
                         logger.info("挪用API 消費時光(單元:毫秒):" + (endTime - startTime));
                        if(statusCode != HttpStatus.SC_OK){
                            logger.error("Method failed:"+response.getStatusLine());
                            status = 1;
                        }

                            //Read the response body
                             body=EntityUtils.toString(response.getEntity());

   }catch(IOException e){
    //產生收集異常
    logger.error("exception occurred!\n"+ExceptionUtils.getFullStackTrace(e));
    //收集毛病
    status = 3;
   }
                    finally{
                        logger.info("挪用接口狀況:" + status);
                    }

   
  }
  return body;
 }

 /**
  * 0.勝利 1.履行辦法掉敗 2.協定毛病 3.收集毛病
  * @return the status
  */
 public int getStatus() {
  return status;
 }

 /**
  * @param status the status to set
  */
 public void setStatus(int status) {
  this.status = status;
 }
 /**
  * @return the startTime
  */
 public long getStartTime() {
  return startTime;
 }
 /**
  * @return the endTime
  */
 public long getEndTime() {
  return endTime;
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved