http 請求,get,post,put請求方式,《利用HttpClient》
阿新 • • 發佈:2018-12-18
簡介:利用nginx 跳過證書,然後java程式碼呼叫nginx
工具類請求程式碼:
/** * httpClient的get請求方式 * * @return * @throws Exception */ public String doGet(String url) throws Exception { /* 1 生成 HttpClinet 物件並設定引數 */ HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url.trim()); getMethod.setRequestHeader("Content-Type", "application/json"); getMethod.setRequestHeader("Host",“放”); getMethod.setRequestHeader("Authorization", "Token "+bizConf.get("INDelhivery_Token").trim()); String response = ""; /* 2 執行 HTTP GET 請求 */ try { int statusCode = httpClient.executeMethod(getMethod); /* 3 判斷訪問的狀態碼 */ if (statusCode != HttpStatus.SC_OK) { System.err.println("請求出錯: " + getMethod.getStatusLine()); } // 讀取 HTTP 響應內容,這裡簡單列印網頁內容 byte[] responseBody = getMethod.getResponseBody();// 讀取為位元組陣列 response = new String(responseBody, "UTF-8"); } finally { /* 4 .釋放連線 */ getMethod.releaseConnection(); } return response; } /** * httpClient的post請求方式 * * @return * @throws Exception */ public String doPost(String jsonData, String url) throws Exception { String postResult = ""; //第一步定義好httpClient HttpClient httpClient = new HttpClient(); //第二步定義好postMethod PostMethod postMethod = new PostMethod(url.trim()); //第三步定義頭 postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); postMethod.setRequestHeader("Host", bizConf.get("INDelhivery_Host")); postMethod.setRequestHeader("Authorization", "Token "+bizConf.get("INDelhivery_Token").trim()); //第四步封裝請求引數 postMethod.setParameter("format", "json"); postMethod.setParameter("data", jsonData); try { int statusCode = httpClient.executeMethod(postMethod); if (statusCode != HttpStatus.SC_OK) { logger.info("請求出錯: " + postMethod.getStatusLine()); } //第五步處理返回引數 postResult = IOUtils.toString(postMethod.getResponseBodyAsStream(), "UTF-8"); } finally { //第六步 釋放連線 postMethod.releaseConnection(); } return postResult; } public String doPut(String jsonData, String url) throws Exception { String postResult = ""; HttpClient httpClient = new HttpClient(); PutMethod putMethod = new PutMethod(url.trim()); putMethod.setRequestHeader("Content-Type", "application/json"); putMethod.setRequestHeader("Accept", "*/*"); putMethod.setRequestHeader("User-Agent", "Mozilla 5.0"); putMethod.setRequestHeader("Host", "digitalapi.auspost.com.au"); String accountNumber=bizConf.get("AUPost_accountNumber"); putMethod.setRequestHeader("account-number", accountNumber); putMethod.setRequestHeader("Cache-Control", "no-cache"); // 獲取加密結果並轉BASE64 putMethod.setRequestHeader("Authorization", getHeader()); // 封裝請求引數 StringRequestEntity requestEntity = new StringRequestEntity(jsonData, "application/json", "UTF-8"); putMethod.setRequestEntity(requestEntity); try { StringBuffer sb = new StringBuffer(); int statusCode = httpClient.executeMethod(putMethod); InputStream is = putMethod.getResponseBodyAsStream(); byte[] b = new byte[1024]; int len = 0; while((len = is.read(b)) > 0) { sb.append(new String(b, 0, len)); } postResult = sb.toString(); } finally { putMethod.releaseConnection(); } return postResult; }