Java項(xiàng)目中如何使用HttpClient發(fā)送一個(gè)Post請(qǐng)求?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)公司主營(yíng)皇姑網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開(kāi)發(fā),皇姑h5小程序設(shè)計(jì)搭建,皇姑網(wǎng)站營(yíng)銷(xiāo)推廣歡迎皇姑等地區(qū)企業(yè)咨詢(xún)
基于項(xiàng)目需求,想要實(shí)現(xiàn)Post消息推送,故采用HttpClient組件進(jìn)行實(shí)現(xiàn),相關(guān)代碼如下(注:程序采用的httpclient和httpcore依賴(lài)包的版本為4.2.5):
import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import java.util.UUID; import net.sf.json.JSONObject; import java.nio.charset.Charset; public static boolean httpPostWithJson(JSONObject jsonObj,String url,String appId){ boolean isSuccess = false; HttpPost post = null; try { HttpClient httpClient = new DefaultHttpClient(); // 設(shè)置超時(shí)時(shí)間 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000); post = new HttpPost(url); // 構(gòu)造消息頭 post.setHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Connection", "Close"); String sessionId = getSessionId(); post.setHeader("SessionId", sessionId); post.setHeader("appid", appid); // 構(gòu)建消息實(shí)體 StringEntity entity = new StringEntity(jsonObj.toString(), Charset.forName("UTF-8")); entity.setContentEncoding("UTF-8"); // 發(fā)送Json格式的數(shù)據(jù)請(qǐng)求 entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = httpClient.execute(post); // 檢驗(yàn)返回碼 int statusCode = response.getStatusLine().getStatusCode(); if(statusCode != HttpStatus.SC_OK){ LogUtil.info("請(qǐng)求出錯(cuò): "+statusCode); isSuccess = false; }else{ int retCode = 0; String sessendId = ""; // 返回碼中包含retCode及會(huì)話(huà)Id for(Header header : response.getAllHeaders()){ if(header.getName().equals("retcode")){ retCode = Integer.parseInt(header.getValue()); } if(header.getName().equals("SessionId")){ sessendId = header.getValue(); } } if(ErrorCodeHelper.IAS_SUCCESS != retCode ){ // 日志打印 LogUtil.info("error return code, sessionId: "sessendId"\t"+"retCode: "+retCode); isSuccess = false; }else{ isSuccess = true; } } } catch (Exception e) { e.printStackTrace(); isSuccess = false; }finally{ if(post != null){ try { post.releaseConnection(); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } return isSuccess; } // 構(gòu)建唯一會(huì)話(huà)Id public static String getSessionId(){ UUID uuid = UUID.randomUUID(); String str = uuid.toString(); return str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24); }
Ps: 在使用Hadoop集群進(jìn)行發(fā)送POST請(qǐng)求時(shí),遇到"java.lang.NoSuchFieldError: INSTANCE"的問(wèn)題,此類(lèi)問(wèn)題一般是"jar包沖突"的問(wèn)題所致,但奇怪的是本地的pom.xml設(shè)置的依賴(lài)包中有該字段,相關(guān)的httpclient依賴(lài)包如下:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.1</version> </dependency>
隨后在網(wǎng)上查找了一翻,找到問(wèn)題的緣由,原因在于Hadoop集群運(yùn)行程序時(shí),首先會(huì)加載自己相關(guān)目錄下的jar包,在自己目錄下如果未找到,才會(huì)加載程序運(yùn)行時(shí)指定的jar包,隨查找了Hadoop集群中相關(guān)Jar包路徑,發(fā)現(xiàn)httpclient的相關(guān)依賴(lài)包為4.2.5,因此將pom.xml配置文件也更新為該版本,程序則運(yùn)行通過(guò).
看完上述內(nèi)容,你們掌握J(rèn)ava項(xiàng)目中如何使用HttpClient發(fā)送一個(gè)Post請(qǐng)求的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱(chēng):Java項(xiàng)目中如何使用HttpClient發(fā)送一個(gè)Post請(qǐng)求
文章路徑:http://www.chinadenli.net/article4/gcipie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站收錄、建站公司、網(wǎng)站設(shè)計(jì)公司、電子商務(wù)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)