https工具類設(shè)置請(qǐng)求頭妙同,使用apache的httpclient實(shí)現(xiàn)

?在項(xiàng)目開發(fā)過程中,我想大部分系統(tǒng)都需要對(duì)接另外的系統(tǒng)粥帚。對(duì)接方式有很多種芒涡,現(xiàn)在最常見的就是https請(qǐng)求了。現(xiàn)將Java發(fā)送https請(qǐng)求的工具類整理一下费尽,有需要用到的同學(xué)拿走不謝。

? 該方法使用apache的httpclient實(shí)現(xiàn)

第一步:創(chuàng)建SSLClient

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ClientConnectionManager;

import org.apache.http.conn.scheme.Scheme;

import org.apache.http.conn.scheme.SchemeRegistry;

import org.apache.http.conn.ssl.SSLSocketFactory;

import org.apache.http.impl.client.DefaultHttpClient;

public class SSLClient extends DefaultHttpClient{

public SSLClient() throws Exception{

super();

SSLContext ctx = SSLContext.getInstance("TLS");

X509TrustManager tm = new X509TrustManager() {

@Override

public void checkClientTrusted(X509Certificate[] chain,

String authType) throws CertificateException {

}

@Override

public void checkServerTrusted(X509Certificate[] chain,

String authType) throws CertificateException {

}

@Override

public X509Certificate[] getAcceptedIssuers() {

return null;

}

};

ctx.init(null, new TrustManager[]{tm}, null);

SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

ClientConnectionManager ccm = this.getConnectionManager();

SchemeRegistry sr = ccm.getSchemeRegistry();

sr.register(new Scheme("https", 443, ssf));

}

}

第二步:實(shí)現(xiàn)自己的https工具類

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.message.BasicHeader;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import java.util.Map;

public class HttpsClientUtil {

private static final String CHAREST = "utf-8";

/**

* 發(fā)送post請(qǐng)求

* @param url

* @return

*/

public static String doPost(String url,String mapParam){

HttpClient httpClient = null;

HttpPost httpPost = null;

String result = null;

try{

httpClient = new SSLClient();

httpPost = new HttpPost(url);

//設(shè)置參數(shù)

httpPost.setHeader("Content-Type", "application/json");

httpPost.addHeader("Authorization", "Basic YWRtaW46");

StringEntity s = new StringEntity(mapParam, CHAREST);

s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));

httpPost.setEntity(s);

HttpResponse response = httpClient.execute(httpPost);

if(response != null){

HttpEntity resEntity = response.getEntity();

if(resEntity != null){

result = EntityUtils.toString(resEntity,CHAREST);

}

}

}catch(Exception ex){

ex.printStackTrace();

}

return result;

}

/**

* 發(fā)送get請(qǐng)求

* @param url? ? ? 鏈接地址

* @return

*/

public static String doGet(String url){

HttpClient httpClient = null;

HttpGet httpGet= null;

String result = null;

try {

httpClient = new SSLClient();

httpGet = new HttpGet(url);

HttpResponse response = httpClient.execute(httpGet);

if(response != null){

HttpEntity resEntity = response.getEntity();

if(resEntity != null){

result = EntityUtils.toString(resEntity,CHAREST);

}

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 發(fā)送get請(qǐng)求,并設(shè)置get的請(qǐng)求頭

* @param url? ? ? 鏈接地址

* @return

*/

public static String setHeadDoGet(String url,Map headers){

HttpClient httpClient = null;

HttpGet httpGet= null;

String result = null;

try {

httpClient = new SSLClient();

httpGet = new HttpGet(url);

for (Map.Entry e : headers.entrySet()) {

httpGet.addHeader(e.getKey(), e.getValue());

}

HttpResponse response = httpClient.execute(httpGet);

if(response != null){

HttpEntity resEntity = response.getEntity();

if(resEntity != null){

result = EntityUtils.toString(resEntity,CHAREST);

}

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

}

ps:有需要設(shè)置post請(qǐng)求頭的請(qǐng)求冬三,同學(xué)們可以參考setHeadDoGet方法進(jìn)行編寫。需要用到的apache的httpclient-4.5.9.jar包长豁。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末匠襟,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子酸舍,更是在濱河造成了極大的恐慌,老刑警劉巖忽舟,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叮阅,死亡現(xiàn)場離奇詭異,居然都是意外死亡浩姥,警方通過查閱死者的電腦和手機(jī)状您,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來眯分,“玉大人柒桑,你說我怎么就攤上這事∧豢眩” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長仇奶。 經(jīng)常有香客問我比驻,道長岛抄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任掸掸,我火速辦了婚禮蹭秋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘仁讨。我一直安慰自己,他們只是感情好盐固,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布丈挟。 她就那樣靜靜地躺著,像睡著了一般长酗。 火紅的嫁衣襯著肌膚如雪桐绒。 梳的紋絲不亂的頭發(fā)上之拨,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音烁竭,去河邊找鬼吉挣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛睬魂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播氯哮,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼姆打!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起玛追,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤闲延,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后邢笙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體侍匙,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年妇汗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了说莫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡互婿,死狀恐怖辽狈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情刮萌,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布壮锻,位于F島的核電站涮阔,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏途事。R本人自食惡果不足惜验懊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一义图、第九天 我趴在偏房一處隱蔽的房頂上張望召烂。 院中可真熱鬧,春花似錦奏夫、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至呛哟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間榛鼎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來泰國打工者娱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留苏揣,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像吐葱,于是被迫代替她去往敵國和親校翔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子弟跑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353