HttpClient

package com.croot.ta.controller;

import java.util.HashMap;

import java.util.Map;

import org.springframework.http.HttpHeaders;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.croot.ta.rest.Response;

import com.croot.ta.utils.HttpClientUtil;

/**

* ?

*

* @author ?wulei

* @ClassName: TaAccountInfoController?

* @Description: TA 賬號(hào)信息

* @date 2018年10月25日 下午1:42:53?

*/

@RestController

@RequestMapping("httpclient")

public class HttpClientTestController {

? ? @RequestMapping("/post")

? ? public Response restpost() {

? ? ? ? // 存放請(qǐng)求參數(shù)集合

? ? ? ? Map<String, String> params = new HashMap<>(3);

? ? ? ? params.put("productNum", "2");

? ? ? ? params.put("productType", "");

? ? ? ? params.put("schemeId", "a2f35119");

? ? ? ? /*String url = "http://192.168.1.43:8084/TA/scheme/performance/selectDetail";*/

? ? ? ? String url = "http://192.168.8.32:8080/TA/scheme/performance/selectDetail";

? ? ? ? String result = HttpClientUtil.doPost(url, params);

? ? ? ? System.out.println(result);

? ? ? ? return null;

? ? }

? ? @RequestMapping("/get")

? ? public Response restget() {

? ? ? ? // 存放請(qǐng)求參數(shù)集合

? ? ? ? Map<String, String> params = new HashMap<>(3);

? ? ? ? params.put("acctID", "JY1000000004");

? ? ? ? params.put("custID", "200010000007");

? ? ? ? params.put("custFundAcctID", "100020000008");

? ? ? ? /* String url =

? ? ? ? ? ? "http://192.168.8.32:8080/TA/accountInfo/getAccountInfo?acctID={acctID}&custID={custID}&custFundAcctID={custFundAcctID}";

? ? ? ? */

? ? ? ? String url = "http://192.168.8.32:8080/TA/accountInfo/getAccountInfo";

? ? ? ? HttpHeaders headers = new HttpHeaders();

? ? ? ? headers.add("uuid",

? ? ? ? ? ? "crooteyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjcm9vdCIsInN1YiI6Ijk5OTkwIiwiZXhwIjoxNTQ4ODEwNzI3LCJuYmYiOjE1NDgyMDU5Mjd9.vAkE0UZNIkMvVnUCD5ijHHFC1Wmo0129FJiYvO9w1cA");

? ? ? ? String result = HttpClientUtil.doGet(url, params);

? ? ? ? System.out.println(result);

? ? ? ? return null;

? ? }

}


package com.croot.ta.utils;

import java.io.IOException;

import java.net.URI;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

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

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

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

import org.apache.http.client.utils.URIBuilder;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.StringEntity;

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

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

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import net.sf.json.JSONObject;

/**

* @Description: 三方接口 調(diào)用工具類

* @author ?wulei

* @date 2019年1月22日 下午5:25:11?

*/

public class HttpClientUtil {

? ? private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

? ? public static String doGet(String url, Map<String, String> param) {

? ? ? ? CloseableHttpClient httpclient = HttpClients.createDefault();

? ? ? ? String resultString = "";

? ? ? ? CloseableHttpResponse response = null;

? ? ? ? try {

? ? ? ? ? ? URIBuilder builder = new URIBuilder(url);

? ? ? ? ? ? if (param != null) {

? ? ? ? ? ? ? ? for (String key : param.keySet()) {

? ? ? ? ? ? ? ? ? ? builder.addParameter(key, param.get(key));

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? URI uri = builder.build();

? ? ? ? ? ? // 創(chuàng)建http GET請(qǐng)求

? ? ? ? ? ? HttpGet httpGet = new HttpGet(uri);

? ? ? ? ? ? httpGet.addHeader("uuid",

? ? ? ? ? ? ? ? "crooteyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjcm9vdCIsInN1YiI6Ijk5OTkwIiwiZXhwIjoxNTQ4ODEwNzI3LCJuYmYiOjE1NDgyMDU5Mjd9.vAkE0UZNIkMvVnUCD5ijHHFC1Wmo0129FJiYvO9w1cA");

? ? ? ? ? ? response = httpclient.execute(httpGet);

? ? ? ? ? ? if (response.getStatusLine().getStatusCode() == 200) {

? ? ? ? ? ? ? ? resultString = EntityUtils.toString(response.getEntity(), "UTF-8");

? ? ? ? ? ? }

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? } finally {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? if (response != null) {

? ? ? ? ? ? ? ? ? ? response.close();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? httpclient.close();

? ? ? ? ? ? } catch (IOException e) {

? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? logger.debug(resultString);

? ? ? ? return resultString;

? ? }

? ? public static String doGet(String url) {

? ? ? ? return doGet(url, null);

? ? }

? ? public static String doPostTime(String url, Map<String, String> param) {

? ? ? ? // 創(chuàng)建Httpclient對(duì)象

? ? ? ? CloseableHttpClient httpClient = HttpClients.createDefault();

? ? ? ? CloseableHttpResponse response = null;

? ? ? ? String resultString = "{\"result\":\"false\"}";

? ? ? ? try {

? ? ? ? ? ? // 創(chuàng)建Http Post請(qǐng)求

? ? ? ? ? ? HttpPost httpPost = new HttpPost(url);

? ? ? ? ? ? // 創(chuàng)建參數(shù)列表

? ? ? ? ? ? if (param != null) {

? ? ? ? ? ? ? ? List<NameValuePair> paramList = new ArrayList<>();

? ? ? ? ? ? ? ? for (String key : param.keySet()) {

? ? ? ? ? ? ? ? ? ? paramList.add(new BasicNameValuePair(key, param.get(key)));

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? // 模擬表單

? ? ? ? ? ? ? ? UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");

? ? ? ? ? ? ? ? httpPost.setEntity(entity);

? ? ? ? ? ? }

? ? ? ? ? ? // 執(zhí)行http請(qǐng)求

? ? ? ? ? ? response = httpClient.execute(httpPost);

? ? ? ? ? ? resultString = EntityUtils.toString(response.getEntity(), "utf-8");

? ? ? ? ? ? System.out.println(resultString);

? ? ? ? ? ? System.out.println("{\"result\":\"true\"}");

? ? ? ? ? ? System.out.println("===================");

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? resultString = "{\"result\":\"false\"}";

? ? ? ? ? ? logger.debug("發(fā)送請(qǐng)求到業(yè)務(wù)系統(tǒng)失敗!請(qǐng)檢查網(wǎng)絡(luò)連接設(shè)置!");

? ? ? ? } finally {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? if (response != null) {

? ? ? ? ? ? ? ? ? ? response.close();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } catch (IOException e) {

? ? ? ? ? ? ? ? resultString = "{\"result\":\"false\"}";

? ? ? ? ? ? ? ? logger.debug("發(fā)送請(qǐng)求到業(yè)務(wù)系統(tǒng)失斮炻怠!請(qǐng)檢查網(wǎng)絡(luò)連接設(shè)置宣渗!");

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return resultString;

? ? }

? ? public static String doPost(String url, Map<String, String> param) {

? ? ? ? // 創(chuàng)建Httpclient對(duì)象

? ? ? ? CloseableHttpClient httpClient = HttpClients.createDefault();

? ? ? ? CloseableHttpResponse response = null;

? ? ? ? String resultString = "{\"result\":\"false\"}";

? ? ? ? try {

? ? ? ? ? ? // 創(chuàng)建Http Post請(qǐng)求

? ? ? ? ? ? HttpPost httpPost = new HttpPost(url);

? ? ? ? ? ? // 創(chuàng)建參數(shù)列表

? ? ? ? ? ? if (param != null) {

? ? ? ? ? ? ? ? List<NameValuePair> paramList = new ArrayList<>();

? ? ? ? ? ? ? ? for (String key : param.keySet()) {

? ? ? ? ? ? ? ? ? ? paramList.add(new BasicNameValuePair(key, param.get(key)));

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? // 模擬表單

? ? ? ? ? ? ? ? /*? UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");

? ? ? ? ? ? ? ? httpPost.setEntity(entity);*/

? ? ? ? ? ? ? ? // 模擬傳json

? ? ? ? ? ? ? ? // ************

? ? ? ? ? ? ? ? JSONObject fromObject = JSONObject.fromObject(param);

? ? ? ? ? ? ? ? StringEntity s = new StringEntity(fromObject.toString());

? ? ? ? ? ? ? ? httpPost.setEntity(s);

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

? ? ? ? ? ? ? ? // ************

? ? ? ? ? ? ? ? httpPost.addHeader("uuid",

? ? ? ? ? ? ? ? ? ? "crooteyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjcm9vdCIsInN1YiI6Ijk5OTkwIiwiZXhwIjoxNTQ4ODI3MDgzLCJuYmYiOjE1NDgyMjIyODN9.uJYlhDCyVDjhNijpWZh2uf7OiW1bVNIyxAGbr9tfUFE");

? ? ? ? ? ? }

? ? ? ? ? ? // 執(zhí)行http請(qǐng)求

? ? ? ? ? ? response = httpClient.execute(httpPost);

? ? ? ? ? ? resultString = EntityUtils.toString(response.getEntity(), "utf-8");

? ? ? ? ? ? System.out.println("調(diào)用系統(tǒng)返回json == " + resultString);

? ? ? ? ? ? // JSONObject json = JSONObject.parseObject(resultString);

? ? ? ? ? ? // boolean b = Boolean.parseBoolean(json.get("result").toString());

? ? ? ? ? ? // if(b){

? ? ? ? ? ? // resultString ="{\"result\":\"true\"}";

? ? ? ? ? ? // }

? ? ? ? ? ? // if(!"{\"result\":\"true\"}".equals(resultString)){

? ? ? ? ? ? // resultString ="{\"result\":\"false\"}";

? ? ? ? ? ? // }

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? resultString = "{\"result\":\"false\"}";

? ? ? ? ? ? logger.debug("發(fā)送請(qǐng)求到業(yè)務(wù)系統(tǒng)失斎鄄簟释移!請(qǐng)檢查網(wǎng)絡(luò)連接設(shè)置捣郊!");

? ? ? ? } finally {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? if (response != null) {

? ? ? ? ? ? ? ? ? ? response.close();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } catch (IOException e) {

? ? ? ? ? ? ? ? resultString = "{\"result\":\"false\"}";

? ? ? ? ? ? ? ? logger.debug("發(fā)送請(qǐng)求到業(yè)務(wù)系統(tǒng)失斄杀贰!請(qǐng)檢查網(wǎng)絡(luò)連接設(shè)置呛牲!");

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return resultString;

? ? }

? ? public static String doPost(String url) {

? ? ? ? return doPost(url, null);

? ? }

? ? public static String doPostJson(String url, String json) {

? ? ? ? // 創(chuàng)建Httpclient對(duì)象

? ? ? ? CloseableHttpClient httpClient = HttpClients.createDefault();

? ? ? ? CloseableHttpResponse response = null;

? ? ? ? String resultString = "";

? ? ? ? try {

? ? ? ? ? ? // 創(chuàng)建Http Post請(qǐng)求

? ? ? ? ? ? HttpPost httpPost = new HttpPost(url);

? ? ? ? ? ? // 創(chuàng)建請(qǐng)求內(nèi)容

? ? ? ? ? ? StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);

? ? ? ? ? ? httpPost.setEntity(entity);

? ? ? ? ? ? // 執(zhí)行http請(qǐng)求

? ? ? ? ? ? response = httpClient.execute(httpPost);

? ? ? ? ? ? resultString = EntityUtils.toString(response.getEntity(), "utf-8");

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? } finally {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? response.close();

? ? ? ? ? ? } catch (IOException e) {

? ? ? ? ? ? ? ? // TODO Auto-generated catch block

? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return resultString;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末刮萌,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子娘扩,更是在濱河造成了極大的恐慌着茸,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件琐旁,死亡現(xiàn)場(chǎng)離奇詭異涮阔,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)灰殴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門敬特,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人验懊,你說(shuō)我怎么就攤上這事擅羞∈洌” “怎么了义图?”我有些...
    開(kāi)封第一講書人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)召烂。 經(jīng)常有香客問(wèn)我碱工,道長(zhǎng),這世上最難降的妖魔是什么奏夫? 我笑而不...
    開(kāi)封第一講書人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任怕篷,我火速辦了婚禮,結(jié)果婚禮上酗昼,老公的妹妹穿的比我還像新娘廊谓。我一直安慰自己,他們只是感情好麻削,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布蒸痹。 她就那樣靜靜地躺著春弥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪叠荠。 梳的紋絲不亂的頭發(fā)上匿沛,一...
    開(kāi)封第一講書人閱讀 51,573評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音榛鼎,去河邊找鬼逃呼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛者娱,可吹牛的內(nèi)容都是我干的抡笼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼肺然,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼蔫缸!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起际起,我...
    開(kāi)封第一講書人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤拾碌,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后街望,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體校翔,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年灾前,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了防症。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡哎甲,死狀恐怖蔫敲,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情炭玫,我是刑警寧澤奈嘿,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站吞加,受9級(jí)特大地震影響裙犹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜衔憨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一叶圃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧践图,春花似錦掺冠、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)悍及。三九已至,卻和暖如春接癌,著一層夾襖步出監(jiān)牢的瞬間心赶,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工缺猛, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缨叫,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓荔燎,卻偏偏與公主長(zhǎng)得像耻姥,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子有咨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容