Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求
PS:這個(gè)jar包,在2007年之后就沒有更新過了, 是比較老的版本了。追求新的版本 用HttpComponents 比較好
引入的jar包為:
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>
具體實(shí)現(xiàn)類為:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class HttpClientHelper {
private static Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);
private HttpClientHelper() {
}
/**
* 發(fā)起POST請(qǐng)求
*
* @param url url
* @param paramJson 參數(shù)的json格式
*/
public static String sendPost(String url, String paramJson) {
logger.info("開始發(fā)起POST請(qǐng)求,請(qǐng)求地址為{},參數(shù)為{}", url, paramJson);
// 創(chuàng)建httpClient實(shí)例對(duì)象
HttpClient httpClient = new HttpClient();
// 設(shè)置httpClient連接主機(jī)服務(wù)器超時(shí)時(shí)間:15000毫秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
// 創(chuàng)建post請(qǐng)求方法實(shí)例對(duì)象
PostMethod postMethod = new PostMethod(url);
// 設(shè)置post請(qǐng)求超時(shí)時(shí)間
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
postMethod.addRequestHeader("Content-Type", "application/json");
try {
//json格式的參數(shù)解析
RequestEntity entity = new StringRequestEntity(paramJson, "application/json", "UTF-8");
postMethod.setRequestEntity(entity);
httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
return result;
} catch (IOException e) {
logger.error("POST請(qǐng)求發(fā)出失敗,請(qǐng)求的地址為{},參數(shù)為{},錯(cuò)誤信息為{}", url, paramJson, e.getMessage(), e);
}
return null;
}
/**
* 發(fā)起GET請(qǐng)求
*
* @param urlParam url請(qǐng)求,包含參數(shù)
*/
public static String sendGet(String urlParam) {
logger.info("開始發(fā)起GET請(qǐng)求,請(qǐng)求地址為{}", urlParam);
// 創(chuàng)建httpClient實(shí)例對(duì)象
HttpClient httpClient = new HttpClient();
// 設(shè)置httpClient連接主機(jī)服務(wù)器超時(shí)時(shí)間:15000毫秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
// 創(chuàng)建GET請(qǐng)求方法實(shí)例對(duì)象
GetMethod getMethod = new GetMethod(urlParam);
// 設(shè)置post請(qǐng)求超時(shí)時(shí)間
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
getMethod.addRequestHeader("Content-Type", "application/json");
try {
httpClient.executeMethod(getMethod);
String result = getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
logger.info("返回信息為{}", result);
return result;
} catch (IOException e) {
logger.error("GET請(qǐng)求發(fā)出失敗,請(qǐng)求的地址為{},錯(cuò)誤信息為{}", urlParam, e.getMessage(), e);
}
return null;
}
public static void main(String[] args) {
String url = "https://jiashubing.cn/tencenttest";
String param = "{\"aaa\":\"bbbbbbb\"}";
sendPost(url, param);
String urlParam = "https://jiashubing.cn/talk/document?fileid=1234";
sendGet(urlParam);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java使用httpclient 發(fā)送請(qǐng)求的示例
- java?11新特性HttpClient主要組件及發(fā)送請(qǐng)求示例詳解
- Java通過HttpClient進(jìn)行HTTP請(qǐng)求的代碼詳解
- java中httpclient封裝post請(qǐng)求和get的請(qǐng)求實(shí)例
- JAVA通過HttpClient發(fā)送HTTP請(qǐng)求的方法示例
- Java使用HttpClient實(shí)現(xiàn)Post請(qǐng)求實(shí)例
- java實(shí)現(xiàn)HttpClient異步請(qǐng)求資源的方法
- Java高并發(fā)場(chǎng)景下的 HttpClient請(qǐng)求優(yōu)化實(shí)現(xiàn)
相關(guān)文章
JDK12的新特性之CompactNumberFormat詳解
這篇文章主要介紹了JDK12的新特性之CompactNumberFormat,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Java 實(shí)現(xiàn)簡(jiǎn)易教務(wù)管理系統(tǒng)的代碼
這篇文章主要介紹了Java 實(shí)現(xiàn)簡(jiǎn)易教務(wù)管理系統(tǒng)的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Java中實(shí)現(xiàn)日志記錄的方案總結(jié)
在平時(shí)使用到一些軟件中,比如某寶或者某書,通過記錄用戶的行為來構(gòu)建和分析用戶的行為數(shù)據(jù),這就需要使用到日志系統(tǒng)來存儲(chǔ)或者記錄數(shù)據(jù),小編為大家整理了幾種Java日志方案,希望對(duì)大家有所幫助2024-12-12
詳解Spring MVC3返回JSON數(shù)據(jù)中文亂碼問題解決
本篇文章主要介紹了Spring MVC3返回JSON數(shù)據(jù)中文亂碼問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
HttpUtils 發(fā)送http請(qǐng)求工具類(實(shí)例講解)
下面小編就為大家?guī)硪黄狧ttpUtils 發(fā)送http請(qǐng)求工具類(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類
這篇文章主要介紹了Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類,涉及java隨機(jī)數(shù)與字符串遍歷、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn)
這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn),依賴注入,是IOC的一個(gè)方面,是個(gè)通常的概念,它有多種解釋,這概念是說你不用創(chuàng)建對(duì)象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下2023-07-07

