HttpUtils调用
生活随笔
收集整理的這篇文章主要介紹了
HttpUtils调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 模擬發送HTTP請求* @author 白鹿* @param url* @param paramMap*/public static String sendHttp(String url,String str) {CloseableHttpClient httpClient = HttpClients.createDefault();RequestConfig.custom().setSocketTimeout(1000).setConnectTimeout(1000).build();//設置請求和傳輸超時時間String returnValue="0";try {//HttpGet httpPost = new HttpGet(url);HttpPost httpPost = new HttpPost(url);StringEntity entity=new StringEntity(str,Charset.forName("UTF-8"));entity.setContentType("application/json;charset=UTF-8");httpPost.setEntity(entity);httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");LOG.info("請求地址 " + httpPost.getRequestLine());// 發起請求 并返回請求的響應CloseableHttpResponse response = httpClient.execute(httpPost);LOG.info("----------------------------------------");// 打印響應狀態LOG.info(response.getStatusLine());// 獲取響應對象HttpEntity resEntity = response.getEntity();if (resEntity != null) {returnValue=EntityUtils.toString(resEntity,"UTF-8");}httpPost.releaseConnection();response.close();return returnValue;} catch (Exception e) {//e.printStackTrace();LOG.error("請求失敗", e);} finally {try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}return returnValue;}
總結
以上是生活随笔為你收集整理的HttpUtils调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018HN省队集训
- 下一篇: 觅知网ppt模板_有哪些相见恨晚的PPT