怎么在java中使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷推廣、網(wǎng)站重做改版、海原網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為海原等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫(xiě)桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
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");//解決中文亂碼問(wèn)題 // 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(); } } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享標(biāo)題:怎么在java中使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求
分享網(wǎng)址:http://bm7419.com/article24/jcigje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、關(guān)鍵詞優(yōu)化、App開(kāi)發(fā)、ChatGPT、Google、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)