1. 程式人生 > 其它 >java 中介面呼叫

java 中介面呼叫

String addSerial = HttpUtil.doHttpGet(getUrl()+"/txSns/tx/getAddSerial.do");
//獲取配置檔案中的介面地址
    private String getUrl() {
        Properties prop = new Properties();
        String messageUrl="";
        URL settingsUrl =getClass().getClassLoader().getResource("/config/sysconfig/sysconfig.properties");
        
try(InputStream in =settingsUrl.openStream();) { prop.load(in); messageUrl = prop.getProperty("message.url").trim(); IOUtils.closeQuietly(in); } catch (Exception e) { e.printStackTrace(); } return messageUrl; }
package com.ustc.base.common.util;

import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse; 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.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import net.minidev.json.JSONObject; /** * 網路請求工具類 * */ public class HttpUtil { private static final Logger logger = Logger.getLogger(HttpUtil.class); private static final Integer SUCCESS = 200; private static final String UTF = "UTF-8"; /** * Http Get 請求 * * @param requestUrl 請求地址 * @return 伺服器返回資訊 * @throws Exception */ public static String doHttpGet(String requestUrl) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(requestUrl); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(3000).build();// 設定響應超時時間和連線超時 httpGet.setConfig(requestConfig); CloseableHttpResponse response = null; String result = null; try { response = httpClient.execute(httpGet); result = EntityUtils.toString(response.getEntity()); if (response.getStatusLine().getStatusCode() == HttpUtil.SUCCESS) {// 請求成功 return result; } else {// 請求失敗 throw new Exception("請求成功"); } } catch (Exception e) { logger.error("系統異常", e); logger.error(result); } finally { if (response != null) { try { response.close(); } catch (IOException e) { logger.error("系統異常", e); } } } return result; } /** * Http Get 請求 * * @param requestUrl 請求地址 * @return 伺服器返回資訊 * @throws Exception */ public static String doHttpGet(String requestUrl, String sign, String appCode, String timestamp) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(requestUrl); httpGet.addHeader("sign", sign); httpGet.addHeader("appCode", appCode); httpGet.addHeader("timestamp", timestamp); CloseableHttpResponse response = null; String result = null; try { response = httpClient.execute(httpGet); result = EntityUtils.toString(response.getEntity()); if (response.getStatusLine().getStatusCode() == HttpUtil.SUCCESS) {// 請求成功 return result; } else {// 請求失敗 throw new Exception("請求成功"); } } catch (Exception e) { logger.error("系統異常", e); logger.error(result); } finally { if (response != null) { try { response.close(); } catch (IOException e) { logger.error("系統異常", e); } } } return result; } /** * Http Post 請求 * * @param requestUrl 請求地址 * @param params 請求引數 * @return 伺服器返回資訊 */ public static String doHttpPost(String requestUrl, Map<String, String> params) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(requestUrl); CloseableHttpResponse response = null; String result = null; try { // 設定請求引數 if (params != null && !params.isEmpty()) { List<NameValuePair> pairList = new ArrayList<NameValuePair>(); for (Entry<String, String> entry : params.entrySet()) { pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } httpPost.setEntity(new UrlEncodedFormEntity(pairList, HttpUtil.UTF)); } response = httpClient.execute(httpPost); result = EntityUtils.toString(response.getEntity(), UTF); if (response.getStatusLine().getStatusCode() == HttpUtil.SUCCESS) {// 請求成功 return result; } else {// 請求失敗 throw new Exception("請求失敗"); } } catch (Exception e) { logger.error("系統異常", e); logger.error(result); } finally { if (response != null) { try { response.close(); } catch (IOException e) { logger.error("系統異常", e); } } } return result; } /** * @param requestUrl 請求地址 * @param json 請求引數 * @return 伺服器返回資訊 */ public static String doHttpPost(String requestUrl, JSONObject json) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(requestUrl); CloseableHttpResponse response = null; String result = null; try { // 設定請求引數 if (json != null) { StringEntity entity = new StringEntity(json.toString(), UTF); entity.setContentEncoding(UTF); entity.setContentType("application/json"); httpPost.setEntity(entity); } response = httpClient.execute(httpPost); result = EntityUtils.toString(response.getEntity(), UTF); if (response.getStatusLine().getStatusCode() == HttpUtil.SUCCESS) {// 請求成功 return result; } else {// 請求失敗 throw new Exception("請求失敗"); } } catch (Exception e) { logger.error("系統異常", e); logger.error(result); } finally { if (response != null) { try { response.close(); } catch (IOException e) { logger.error("系統異常", e); } } } return result; } }