java實現(xiàn)http請求工具類示例
通過http rest請求返回數(shù)據
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實例
* 公開,靜態(tài)的工廠方法,需要使用時才去創(chuàng)建該單體
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
}
return httpClient;
}
/**
* POST方式調用
*
* @param url
* @param params 參數(shù)為NameValuePair鍵值對對象
* @return 響應字符串
* @throws java.io.UnsupportedEncodingException
*/
public static String executeByPOST(String url, List<NameValuePair> params) {
HttpClient httpclient = getHttpClient();
HttpPost post = new HttpPost(url);
ResponseHandler<String> responseHandler = 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 帶參數(shù)占位符的URL,例:http://****/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}
* @param params 參數(shù)值數(shù)組,需要與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);
ResponseHandler<String> responseHandler = 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);
ResponseHandler<String> responseHandler = 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;
}
}
相關文章
springboot rocketmq配置生產者和消息者的步驟
本文介紹了如何在Spring Boot中集成RocketMQ,包括添加依賴、配置application.yml、創(chuàng)建生產者和消費者,并展示了如何發(fā)送和接收消息,感興趣的朋友一起看看吧2025-03-03
Springboot使用POI進行excel文件的導出與下載方式
這篇文章主要介紹了Springboot使用POI進行excel文件的導出與下載方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot通過配置Swagger權限解決Swagger未授權訪問漏洞問題
這篇文章主要介紹了SpringBoot通過配置Swagger權限解決Swagger未授權訪問漏洞問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Mybatis下動態(tài)sql中##和$$的區(qū)別講解
今天小編就為大家分享一篇關于Mybatis下動態(tài)sql中##和$$的區(qū)別講解,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Springboot實現(xiàn)發(fā)送郵件及注冊激活步驟
為了方便郵件發(fā)送功能的使用,我們用郵件發(fā)送功能實現(xiàn)用戶注冊,實現(xiàn)步驟大概就是進行用戶注冊同時發(fā)送一封激活郵件,郵件里附帶激活鏈接,關于Springboot發(fā)送郵件注冊激活功能的實現(xiàn)參考下本文吧2021-06-06
SpringBoot 整合 dubbo xml實現(xiàn)代碼示例
這篇文章主要介紹了SpringBoot 整合 dubbo xml實現(xiàn)代碼示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

