糯米文學吧

位置:首頁 > 計算機 > java語言

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

java語言4.26K

通過http rest請求返回數據

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

複製代碼 代碼如下:

import ;

import actory;

import ValuePair;

import ntProtocolException;

import Client;

import onseHandler;

import ncodedFormEntity;

import Get;

import Post;

import cResponseHandler;

import ultHttpClient;

import adSafeClientConnManager;

import ception;

import ageFormat;

import ;

import Unit;

/**

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

*

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

*/

public class HttpClientUtils {

private static final Log log = og(s);

/**

* 初始化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 pportedEncodingException

*/

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) {

ntity(new UrlEncodedFormEntity(params));

}

responseJson = ute(post, responseHandler);

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

} catch (ClientProtocolException e) {

tStackTrace();

("HttpClient POST請求異常:" + essage());

} catch (IOException e) {

tStackTrace();

} finally {

onnectionManager()eExpiredConnections();

onnectionManager()eIdleConnections(30, NDS);

}

return responseJson;

}

/**

* Get方式請求

*

* @param url 帶參數佔位符的URL,例:http://****/User/user/{0}&email={1}

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

* @return 響應字符串

* @throws pportedEncodingException

*/

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

HttpClient httpclient = getHttpClient();

String messages = at(url, params);

HttpGet get = new HttpGet(messages);

ResponseHandlerresponseHandler = new BasicResponseHandler();

String responseJson = null;

try {

responseJson = ute(get, responseHandler);

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

} catch (ClientProtocolException e) {

tStackTrace();

("HttpClient GET請求異常:" + essage());

} catch (IOException e) {

tStackTrace();

("HttpClient GET請求異常:" + essage());

} finally {

onnectionManager()eExpiredConnections();

onnectionManager()eIdleConnections(30, NDS);

}

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 = ute(get, responseHandler);

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

} catch (ClientProtocolException e) {

tStackTrace();

("HttpClient GET請求異常:" + essage());

} catch (IOException e) {

tStackTrace();

("HttpClient GET請求異常:" + essage());

} finally {

onnectionManager()eExpiredConnections();

onnectionManager()eIdleConnections(30, NDS);

}

return responseJson;

}

}

標籤:示例 JAVA Http