CloseableHttpClient獲取返回結果



import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.Gson;
/** * 接口簽名演示類 */ public class SignatureDemo { private static final Logger logger = LoggerFactory.getLogger(SignatureDemo.class); // 接口服務地址 static String restSever = "http://192.168.10.173:11080/ums/service/V1/res/users"; // 身份系統簽發給應用對接的密鑰 static String pwd = "ZNn2fdrvSWR+aGN2ORe/CtMfIq4yuJSzFBtVuaqVnUEJ457QB3W7l0T9yslAMr3H"; public static void main(String[] args) { // 創建HttpClient對象 CloseableHttpClient httpclient = HttpClients.createDefault(); /** * GET查詢接口演示代碼 */ HttpGet httpGet = new HttpGet(getFullUrl); // 對參數簽名,並放入請求header中的signData參數中 try { String urlStr = httpGet.getURI().toString(); if (StringUtils.endsWith(urlStr, "/")) { urlStr = StringUtils.removeEnd(urlStr, "/"); } httpGet.setURI(new URI(urlStr)); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000) .setConnectionRequestTimeout(3000).setSocketTimeout(3000).build(); httpGet.setConfig(requestConfig); // 執行請求 CloseableHttpResponse response = httpclient.execute(httpGet); // 取響應的結果 int statusCode = response.getStatusLine().getStatusCode(); // 打印響應結果 if (statusCode == HttpStatus.SC_OK) { String resp = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println("status:" + statusCode); System.out.println("result:" + resp); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /** * PUT修改接口的演示代碼,POST與PUT類似 */ String ID = "aa03a5c692cf480b87887e0ff8cfe566"; // 這里若是通過ID查詢的,接口中ID是作為路徑存在的,所以需要將ID組合成 String putQueryParam = "ID=" + ID; String putFullUrl = restSever + "/" + ID; // 訪問用戶接口 HttpPut httpPut = new HttpPut(putFullUrl); // 模擬POST/PUT的body中數據,需轉為JSON進行簽名。GET則沒有這部分內容。 Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("USER_NAME", "張三"); String bodyParam = new Gson().toJson(dataMap); String postAllParamUrl = commonParamUrl + "&" + putQueryParam + "&bodyData=" + bodyParam; StringEntity bodyData = new StringEntity(bodyParam.toString(), "UTF-8"); httpPut.setEntity(bodyData); try { String urlStr = httpPut.getURI().toString(); // 公共參數URL System.out.println("commonParamter:" + urlStr); if (StringUtils.endsWith(urlStr, "/")) { urlStr = StringUtils.removeEnd(urlStr, "/"); } httpPut.setURI(new URI(urlStr)); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000) .setConnectionRequestTimeout(3000).setSocketTimeout(3000).build(); httpPut.setConfig(requestConfig); System.out.println("urlStr in request:" + httpPut.getURI().toString()); // 執行請求 CloseableHttpResponse response = httpclient.execute(httpPut); // 取響應的結果 int statusCode = response.getStatusLine().getStatusCode(); // 打印響應結果 if (statusCode == HttpStatus.SC_OK) { String resp = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println("status:" + statusCode); System.out.println("result:" + resp); } } catch (URISyntaxException e) { logger.error("簽名失敗:", e); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM