java使用common-httpclient包實現(xiàn)post請求方法示例
前言
項目中需要請求第三方接口,而且要求請求參數(shù)數(shù)據(jù)為json類型的。本來首先使用的是httpclient的jar包,但是因為項目中已經(jīng)使用了common-httpclient的jar包,引起了沖突,所以不得不使用common-httpclient來實現(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請求
* @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("請求接口參數(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("請求接口路徑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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Mybatis #foreach中相同的變量名導致值覆蓋的問題解決
本文主要介紹了Mybatis #foreach中相同的變量名導致值覆蓋的問題解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
最新SpringCloud?Stream消息驅(qū)動講解
SpringCloud Stream 是一個構(gòu)建消息驅(qū)動微服務的框架,通過 SpringCloud Stream 連接消息中間件,以實現(xiàn)消息事件驅(qū)動,這篇文章主要介紹了SpringCloud?Stream消息驅(qū)動,需要的朋友可以參考下2022-11-11
java多線程編程之向線程傳遞數(shù)據(jù)的三種方法
在多線程的異步開發(fā)模式下,數(shù)據(jù)的傳遞和返回和同步開發(fā)模式有很大的區(qū)別。由于線程的運行和結(jié)束是不可預料的,因此,在傳遞和返回數(shù)據(jù)時就無法象函數(shù)一樣通過函數(shù)參數(shù)和return語句來返回數(shù)據(jù)2014-01-01
一文搞懂SpringMVC中@InitBinder注解的使用
@InitBinder方法可以注冊控制器特定的java.bean.PropertyEditor或Spring Converter和 Formatter組件。本文通過示例為大家詳細講講@InitBinder注解的使用,需要的可以參考一下2022-06-06
java基于quasar實現(xiàn)協(xié)程池的方法示例
本文主要介紹了java基于quasar實現(xiàn)協(xié)程池的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧<BR>2022-06-06
SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper實例詳解
這篇文章主要介紹了SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper實例詳解 ,需要的朋友可以參考下2017-09-09

