java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求方法示例
前言
項(xiàng)目中需要請(qǐng)求第三方接口,而且要求請(qǐng)求參數(shù)數(shù)據(jù)為json類型的。本來首先使用的是httpclient的jar包,但是因?yàn)轫?xiàng)目中已經(jīng)使用了common-httpclient的jar包,引起了沖突,所以不得不使用common-httpclient來實(shí)現(xiàn)。
示例代碼:
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
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.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HTTPUtils {
private static Logger logger = LoggerFactory.getLogger(HTTPUtils.class);
/**
* post請(qǐng)求
* @param url
* @param json
* @return
*/
public static String postJosnContent(String url, String Json) throws Exception {
// HttpPost method = new HttpPost(url);
// DefaultHttpClient httpClient = new DefaultHttpClient();
// String ret = null;
// try {
// StringEntity entity = new StringEntity(Json,"UTF-8");//解決中文亂碼問題
// entity.setContentEncoding("UTF-8");
// entity.setContentType("application/json");
// method.setEntity(entity);
// HttpResponse result = httpClient.execute(method);
// ret = EntityUtils.toString(result.getEntity());
// } catch (Exception e) {
// throw e;
// } finally {
// method.releaseConnection();
// }
// return ret;
logger.error("請(qǐng)求接口參數(shù):" + Json);
PostMethod method = new PostMethod(url);
HttpClient httpClient = new HttpClient();
try {
RequestEntity entity = new StringRequestEntity(Json,"application/json","UTF-8");
method.setRequestEntity(entity);
httpClient.executeMethod(method);
logger.error("請(qǐng)求接口路徑url:" + method.getURI().toString());
InputStream in = method.getResponseBodyAsStream();
//下面將stream轉(zhuǎn)換為String
StringBuffer sb = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
char[] b = new char[4096];
for(int n; (n = isr.read(b)) != -1;) {
sb.append(new String(b, 0, n));
}
String returnStr = sb.toString();
return returnStr;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
method.releaseConnection();
}
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
mybatis-plus配置控制臺(tái)打印完整帶參數(shù)SQL語句的實(shí)現(xiàn)
這篇文章主要介紹了mybatis-plus配置控制臺(tái)打印完整帶參數(shù)SQL語句,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
springcloud整合到項(xiàng)目中無法啟動(dòng)報(bào)錯(cuò)Failed to start bean&n
這篇文章主要介紹了springcloud整合到項(xiàng)目中無法啟動(dòng)報(bào)錯(cuò)Failed to start bean 'eurekaAutoServiceRegistration'問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot如何接收Post請(qǐng)求Body里面的參數(shù)
這篇文章主要介紹了SpringBoot如何接收Post請(qǐng)求Body里面的參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
通過pipeline配置sonar自動(dòng)化實(shí)現(xiàn)過程解析
這篇文章主要介紹了通過pipeline配置sonar自動(dòng)化實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題
這篇文章主要介紹了解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題,項(xiàng)目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12
SpringBoot使用Feign進(jìn)行服務(wù)間通信的實(shí)現(xiàn)示例代碼
Feign是一個(gè)開源的Java HTTP客戶端,可以幫助我們?cè)赟pringBoot應(yīng)用中快速構(gòu)建和使用HTTP客戶端,方便實(shí)現(xiàn)服務(wù)間的通信,本文就來介紹一下SpringBoot使用Feign進(jìn)行服務(wù)間通信的實(shí)現(xiàn)示例代碼,感興趣的可以了解一下2024-01-01

