HttpClientUtil

package com..utils;

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.nio.charset.Charset;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

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.impl.client.DefaultHttpClient;

import lombok.extern.slf4j.Slf4j;

/*

* httpclientUtil

*

*/

@Slf4j

public class HttpClientUtil {

/**

? ? * post

? ? *

? ? * @param sendData

? ? * @param url

? ? * @return

? ? * @throws Exception

*/

? public static String doPost(String sendData, String url, Map header)throws Exception {

String result =null;

try {

HttpResponse response =postRes(sendData, url, header);

HttpEntity httpEntity = response.getEntity();

InputStream instreams = httpEntity.getContent();

result =convertStreamToString(instreams);

}catch (Exception ex) {

throw ex;

}

return result;

}

/**

? ? * post response

*

? ? * @param sendData

? ? * @param url

? ? * @return

? ? * @throws Exception

*/

? public static HttpResponse postRes(String sendData, String url, Map header)throws Exception {

HttpClient httpClient =null;

HttpPost httpPost =null;

httpClient =new DefaultHttpClient();

httpPost =new HttpPost(url);

//? ? httpPost.addHeader("Content-type", "application/json; charset=utf-8");

//? ? httpPost.setHeader("Accept", "application/json");

? ? ? StringEntity entity =new StringEntity(sendData, Charset.forName("UTF-8"));

//? ? entity.setContentEncoding("UTF-8");

//? ? entity.setContentType("application/json");

? ? ? if (header !=null && !header.isEmpty()) {

for (Map.Entry map : header.entrySet()) {

httpPost.addHeader(map.getKey(), map.getValue());

}

}

httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);

return response;

}

/**

? ? * httpClient get

? ? *

? ? * @param url

? ? * @return

? ? * @throws Exception

*/

? public static String doGet(String url, Map header)throws Exception {

String result =null;

try {

HttpResponse response =getRes(url, header);

HttpEntity httpEntity = response.getEntity();

if (null != httpEntity) {

InputStream instreams = httpEntity.getContent();

result =convertStreamToString(instreams);

}

}catch (Exception e) {

throw e;

}

return result;

}

/**

? ? * httpClient get response

*

? ? * @param url

? ? * @return

? ? * @throws Exception

*/

? public static HttpResponse getRes(String url, Map header)throws Exception {

HttpClient httpClient =null;

HttpGet httpGet =null;

httpClient =new DefaultHttpClient();

httpGet =new HttpGet(url);

if (header !=null && !header.isEmpty()) {

for (Map.Entry map : header.entrySet()) {

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

}

}

System.out.println("get method url-------------------------->>>" + httpGet.getURI());

HttpResponse response = httpClient.execute(httpGet);

return response;

}

public static String convertStreamToString(InputStream is, HttpServletResponse response,boolean record,

String contenyType)throws IOException {

StringBuilder sb1 =new StringBuilder();

if (!record) {

sb1.append("此方法為:").append(contenyType).append(',');

}

ByteArrayOutputStream baos =new ByteArrayOutputStream();

try {

IOUtils.copy(is, baos);

if (record) {

String str =new String(baos.toByteArray(),"UTF-8");

sb1.append(str);

}else {

sb1.append("長(zhǎng)度:").append(baos.toByteArray().length);

}

response.getOutputStream().write(baos.toByteArray());

response.getOutputStream().flush();

}catch (IOException e) {

throw e;

}

return sb1.toString();

}

public static String convertStreamToString(InputStream is)throws IOException {

StringBuilder sb1 =new StringBuilder();

byte[] bytes =new byte[4096];

int size =0;

try {

while ((size = is.read(bytes)) >0) {

String str =new String(bytes,0, size,"UTF-8");

sb1.append(str);

}

}catch (IOException e) {

throw e;

}finally {

try {

is.close();

}catch (IOException e) {

throw e;

}

}

return sb1.toString();

}

/**

? ? *?

? ? *

? ? * @param request

? ? * @return

? ? * @throws IOException

*/

? public static String requestBody(HttpServletRequest request)throws IOException {

ByteArrayOutputStream bos =new ByteArrayOutputStream();

IOUtils.copy(request.getInputStream(),bos);

//? ? BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

//? ? String line;

//? ? StringBuilder sb = new StringBuilder();

//? ? while ((line = br.readLine()) != null) {

//? ? ? sb.append(line).append('\n');

//? ? }

//? ? String rsb = sb.toString();

? ? ? String rsb =new String(bos.toByteArray(),"utf-8");

if(rsb.length()>200) {

log.info("request body length {} b:{}",bos.size(), rsb.substring(0,199));

}else {

log.info("request body length {} b:{}",bos.size(), rsb);

}

return rsb;

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末吩谦,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子膝藕,更是在濱河造成了極大的恐慌式廷,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件芭挽,死亡現(xiàn)場(chǎng)離奇詭異滑废,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)袜爪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)蠕趁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人辛馆,你說(shuō)我怎么就攤上這事俺陋。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵腊状,是天一觀的道長(zhǎng)诱咏。 經(jīng)常有香客問(wèn)我,道長(zhǎng)缴挖,這世上最難降的妖魔是什么袋狞? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮映屋,結(jié)果婚禮上硕并,老公的妹妹穿的比我還像新娘。我一直安慰自己秧荆,他們只是感情好倔毙,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著乙濒,像睡著了一般陕赃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上颁股,一...
    開(kāi)封第一講書(shū)人閱讀 52,441評(píng)論 1 310
  • 那天么库,我揣著相機(jī)與錄音,去河邊找鬼甘有。 笑死诉儒,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的亏掀。 我是一名探鬼主播忱反,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼滤愕!你這毒婦竟也來(lái)了温算?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤间影,失蹤者是張志新(化名)和其女友劉穎注竿,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體魂贬,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡巩割,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了付燥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宣谈。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖机蔗,靈堂內(nèi)的尸體忽然破棺而出蒲祈,到底是詐尸還是另有隱情,我是刑警寧澤萝嘁,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布梆掸,位于F島的核電站,受9級(jí)特大地震影響牙言,放射性物質(zhì)發(fā)生泄漏酸钦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一咱枉、第九天 我趴在偏房一處隱蔽的房頂上張望卑硫。 院中可真熱鬧,春花似錦蚕断、人聲如沸欢伏。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)硝拧。三九已至,卻和暖如春葛假,著一層夾襖步出監(jiān)牢的瞬間障陶,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工聊训, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留抱究,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓带斑,卻偏偏與公主長(zhǎng)得像鼓寺,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子勋磕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

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