/**
* Http請求工具類
*
* @author
*
*/
@Slf4j
public class HttpClientUtil
{
private HttpClientUtil()
{
}
// private static Log logger = LogFactory.getLog(HttpClientUtil.class);
/**
* 發(fā)送HTTP_POST請求,json格式數(shù)據(jù)
*
* @param url
* @param body
* @return
* @throws Exception
*/
public static String sendPostByJson(String url, String body) throws Exception {
CloseableHttpClient httpclient = HttpClients.custom().build();
HttpPost post = null;
String resData = null;
CloseableHttpResponse result = null;
try
{
post = new HttpPost(url);
HttpEntity entity2 = new StringEntity(body, Consts.UTF_8);
post.setConfig(RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build());
post.setHeader("Content-Type", "application/json");
post.setHeader("authorization", "APP_KEYS F6D6EB20-ACA1-44BB-A72D-BC92D01BB07E");
post.setEntity(entity2);
result = httpclient.execute(post);
if (HttpStatus.SC_OK == result.getStatusLine().getStatusCode())
{
resData = EntityUtils.toString(result.getEntity());
}
else
{
throw new Exception(EntityUtils.toString(result.getEntity()));
}
}
finally
{
if (result != null)
{
result.close();
}
if (post != null)
{
post.releaseConnection();
}
httpclient.close();
}
return resData;
}
/**
* 請求的時候
*
*
* @param msg
* @return
* @throws Exception
*/
public static Response httpConnection(Object obj, String msg, String url) throws Exception {
String result = HttpClientUtil.sendPostByJson(url, JSON.toJSONString(obj));
Response response = JSON.parseObject(result, Response.class);
// 根據(jù)epc查詢貨品看是否為正確的epc
if ("success".equals(response.getStatus().toLowerCase()))
{
return response;
}
else
{
// 處理顯示失敗信息
throw new DreamRuntimeException(response.getMsg());
}
}
}
HTTP工具類
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門敛惊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來渊鞋,“玉大人,你說我怎么就攤上這事瞧挤∥危” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵特恬,是天一觀的道長执俩。 經(jīng)常有香客問我,道長癌刽,這世上最難降的妖魔是什么役首? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮显拜,結果婚禮上衡奥,老公的妹妹穿的比我還像新娘。我一直安慰自己远荠,他們只是感情好矮固,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著譬淳,像睡著了一般档址。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上邻梆,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼宜咒!你這毒婦竟也來了惠赫?” 一聲冷哼從身側響起,我...
- 正文 年R本政府宣布半醉,位于F島的核電站疚俱,受9級特大地震影響,放射性物質發(fā)生泄漏缩多。R本人自食惡果不足惜呆奕,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望衬吆。 院中可真熱鬧梁钾,春花似錦、人聲如沸逊抡。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽秦忿。三九已至麦射,卻和暖如春蛾娶,著一層夾襖步出監(jiān)牢的瞬間灯谣,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內容
- 前言 在我們日常工作中穆碎,我們需要經(jīng)常和第三方接口進行交互通信,很多時候我們都是使用http協(xié)議進行交互职恳,java原...