Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求
PS:這個(gè)jar包,在2007年之后就沒(méi)有更新過(guò)了, 是比較老的版本了。追求新的版本 用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)類(lèi)為:
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("開(kāi)始發(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("開(kāi)始發(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通過(guò)HttpClient進(jìn)行HTTP請(qǐng)求的代碼詳解
- java中httpclient封裝post請(qǐng)求和get的請(qǐng)求實(shí)例
- JAVA通過(guò)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)文章
Spring MVC 關(guān)于controller的字符編碼問(wèn)題
在使用springMVC框架構(gòu)建web應(yīng)用,客戶端常會(huì)請(qǐng)求字符串、整型、json等格式的數(shù)據(jù),通常使用@ResponseBody注解使 controller回應(yīng)相應(yīng)的數(shù)據(jù)而不是去渲染某個(gè)頁(yè)面。2017-03-03Java類(lèi)和對(duì)象的設(shè)計(jì)原理
這篇文章主要介紹了Java類(lèi)和對(duì)象的設(shè)計(jì)原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07Spring MVC創(chuàng)建項(xiàng)目踩過(guò)的bug
這篇文章主要介紹了Spring MVC創(chuàng)建項(xiàng)目踩過(guò)的bug,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Spring Boot啟動(dòng)時(shí)調(diào)用自己的非web邏輯
在spring Boot中,有些代碼是WEB功能,例如API等,但是有些邏輯是非WEB,啟動(dòng)時(shí)就要調(diào)用并持續(xù)運(yùn)行的,該如何加載自己的非WEB邏輯呢,下面通過(guò)實(shí)例代碼給大家講解,一起看看吧2017-07-07JSON.toJSONString()空字段不忽略修改的問(wèn)題
這篇文章主要介紹了JSON.toJSONString()空字段不忽略修改的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02