<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            java語言

            java實現http請求工具類示例

            時間:2025-02-26 19:36:04 java語言 我要投稿
            • 相關推薦

            關于java實現http請求工具類示例

              通過http rest請求返回數據

              復制代碼 代碼如下:

              import org.apache.commons.logging.Log;

              import org.apache.commons.logging.LogFactory;

              import org.apache.http.NameValuePair;

              import org.apache.http.client.ClientProtocolException;

              import org.apache.http.client.HttpClient;

              import org.apache.http.client.ResponseHandler;

              import org.apache.http.client.entity.UrlEncodedFormEntity;

              import org.apache.http.client.methods.HttpGet;

              import org.apache.http.client.methods.HttpPost;

              import org.apache.http.impl.client.BasicResponseHandler;

              import org.apache.http.impl.client.DefaultHttpClient;

              import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

              import java.io.IOException;

              import java.text.MessageFormat;

              import java.util.List;

              import java.util.concurrent.TimeUnit;

              /**

              * 分裝一個http請求的工具類

              *

              * @author 顧煒【guwei】 on 14-4-22.下午3:17

              */

              public class HttpClientUtils {

              private static final Log log = LogFactory.getLog(HttpClientUtils.class);

              /**

              * 初始化HttpClient

              */

              private static HttpClient httpClient = null;

              /**

              * 生產HttpClient實例

              * 公開,靜態的工廠方法,需要使用時才去創建該單體

              *

              * @return

              */

              public static HttpClient getHttpClient() {

              if (httpClient == null) {

              httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());

              }

              return httpClient;

              }

              /**

              * POST方式調用

              *

              * @param url

              * @param params 參數為NameValuePair鍵值對對象

              * @return 響應字符串

              * @throws java.io.UnsupportedEncodingException

              */

              public static String executeByPOST(String url, Listparams) {

              HttpClient httpclient = getHttpClient();

              HttpPost post = new HttpPost(url);

              ResponseHandlerresponseHandler = new BasicResponseHandler();

              String responseJson = null;

              try {

              if (params != null) {

              post.setEntity(new UrlEncodedFormEntity(params));

              }

              responseJson = httpclient.execute(post, responseHandler);

              log.info("HttpClient POST請求結果:" + responseJson);

              } catch (ClientProtocolException e) {

              e.printStackTrace();

              log.info("HttpClient POST請求異常:" + e.getMessage());

              } catch (IOException e) {

              e.printStackTrace();

              } finally {

              httpclient.getConnectionManager().closeExpiredConnections();

              httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);

              }

              return responseJson;

              }

              /**

              * Get方式請求

              *

              * @param url 帶參數占位符的URL,例:http://****/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}

              * @param params 參數值數組,需要與url中占位符順序對應

              * @return 響應字符串

              * @throws java.io.UnsupportedEncodingException

              */

              public static String executeByGET(String url, Object[] params) {

              HttpClient httpclient = getHttpClient();

              String messages = MessageFormat.format(url, params);

              HttpGet get = new HttpGet(messages);

              ResponseHandlerresponseHandler = new BasicResponseHandler();

              String responseJson = null;

              try {

              responseJson = httpclient.execute(get, responseHandler);

              log.info("HttpClient GET請求結果:" + responseJson);

              } catch (ClientProtocolException e) {

              e.printStackTrace();

              log.info("HttpClient GET請求異常:" + e.getMessage());

              } catch (IOException e) {

              e.printStackTrace();

              log.info("HttpClient GET請求異常:" + e.getMessage());

              } finally {

              httpclient.getConnectionManager().closeExpiredConnections();

              httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);

              }

              return responseJson;

              }

              /**

              * @param url

              * @return

              */

              public static String executeByGET(String url) {

              HttpClient httpclient = getHttpClient();

              HttpGet get = new HttpGet(url);

              ResponseHandlerresponseHandler = new BasicResponseHandler();

              String responseJson = null;

              try {

              responseJson = httpclient.execute(get, responseHandler);

              log.info("HttpClient GET請求結果:" + responseJson);

              } catch (ClientProtocolException e) {

              e.printStackTrace();

              log.info("HttpClient GET請求異常:" + e.getMessage());

              } catch (IOException e) {

              e.printStackTrace();

              log.info("HttpClient GET請求異常:" + e.getMessage());

              } finally {

              httpclient.getConnectionManager().closeExpiredConnections();

              httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);

              }

              return responseJson;

              }

              }

            【java實現http請求工具類示例】相關文章:

            JAVA如何獲取HTTP請求頭10-22

            java如何利用java.net.URLConnection發送HTTP請求08-05

            Java 發送http請求上傳文件功能案例09-11

            java構造函數實現代碼示例08-23

            java system類使用方法示例10-09

            Java基于余弦方法實現的計算相似度算法示例09-03

            PHP實現HTTP斷點續傳的方法07-10

            PHP中Http協議post請求參數10-06

            java運行異常的示例10-31

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看