久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Java commons-httpclient如果實(shí)現(xiàn)get及post請求

瀏覽:19日期:2022-08-24 13:51:01

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請求 * * @param url url * @param paramJson 參數(shù)的json格式 */ public static String sendPost(String url, String paramJson) { logger.info('開始發(fā)起POST請求,請求地址為{},參數(shù)為{}', url, paramJson); // 創(chuàng)建httpClient實(shí)例對象 HttpClient httpClient = new HttpClient(); // 設(shè)置httpClient連接主機(jī)服務(wù)器超時(shí)時(shí)間:15000毫秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000); // 創(chuàng)建post請求方法實(shí)例對象 PostMethod postMethod = new PostMethod(url); // 設(shè)置post請求超時(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請求發(fā)出失敗,請求的地址為{},參數(shù)為{},錯(cuò)誤信息為{}', url, paramJson, e.getMessage(), e); } return null; } /** * 發(fā)起GET請求 * * @param urlParam url請求,包含參數(shù) */ public static String sendGet(String urlParam) { logger.info('開始發(fā)起GET請求,請求地址為{}', urlParam); // 創(chuàng)建httpClient實(shí)例對象 HttpClient httpClient = new HttpClient(); // 設(shè)置httpClient連接主機(jī)服務(wù)器超時(shí)時(shí)間:15000毫秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000); // 創(chuàng)建GET請求方法實(shí)例對象 GetMethod getMethod = new GetMethod(urlParam); // 設(shè)置post請求超時(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請求發(fā)出失敗,請求的地址為{},錯(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); }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 连城县| 鄂托克旗| 广东省| 宜城市| 麟游县| 沈阳市| 运城市| 金堂县| 盐城市| 金平| 五河县| 浦县| 休宁县| 宕昌县| 重庆市| 屏东县| 郓城县| 阿拉善左旗| 大余县| 酒泉市| 桂平市| 怀宁县| 南平市| 祁门县| 五常市| 临猗县| 措勤县| 宜丰县| 高青县| 平舆县| 苏州市| 木里| 祁东县| 陵水| 屏南县| 探索| 绥芬河市| 利辛县| 苏州市| 横峰县| 勃利县|