java實(shí)現(xiàn)http請求工具類示例
通過http rest請求返回?cái)?shù)據(jù)
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;
/**
* 分裝一個(gè)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;
/**
* 生產(chǎn)HttpClient實(shí)例
* 公開,靜態(tài)的工廠方法,需要使用時(shí)才去創(chuàng)建該單體
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
}
return httpClient;
}
/**
* POST方式調(diào)用
*
* @param url
* @param params 參數(shù)為NameValuePair鍵值對對象
* @return 響應(yīng)字符串
* @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請求結(jié)果:" + 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中占位符順序?qū)?yīng)
* @return 響應(yīng)字符串
* @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請求結(jié)果:" + 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請求結(jié)果:" + 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;
}
}
相關(guān)文章
關(guān)于Spring?Data?Jpa?自定義方法實(shí)現(xiàn)問題
這篇文章主要介紹了關(guān)于Spring?Data?Jpa?自定義方法實(shí)現(xiàn)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12詳解SpringBoot初始教程之Tomcat、Https配置以及Jetty優(yōu)化
本篇文章主要介紹了詳解SpringBoot初始教程之Tomcat、Https配置以及Jetty優(yōu)化,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09Java實(shí)現(xiàn)的Base64加密算法示例
這篇文章主要介紹了Java實(shí)現(xiàn)的Base64加密算法,結(jié)合實(shí)例形式分析了Java實(shí)現(xiàn)的base64編碼轉(zhuǎn)換相關(guān)使用方法及操作注意事項(xiàng),需要的朋友可以參考下2018-04-04Springboot如何切換默認(rèn)的Tomcat容器
這篇文章主要介紹了Springboot如何切換默認(rèn)的Tomcat容器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07