HttpClient實現前端請求轉發到另一個服務
阿新 • • 發佈:2020-07-13
import lombok.extern.slf4j.Slf4j; import org.apache.http.*; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut;import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ByteArrayResource; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.util.WebUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Map; @Slf4j public class HttpUtil { public static void httpGet(String url, HttpServletRequest request, HttpServletResponse response, List<NameValuePair> additionalMap) { CloseableHttpClient httpClient = SSLUtils.createSSLClientDefault(); HttpGet httpGet = new HttpGet(RedirectUtil.getHost(url)); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); httpGet.addHeader(headerName, request.getHeader(headerName)); } List<NameValuePair> params = new ArrayList<NameValuePair>(); Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = parameterNames.nextElement(); for (String value : request.getParameterValues(parameterName)) { if (value!=null&&!value.equals("null")) params.add(new BasicNameValuePair(parameterName, value)); } } params.addAll(additionalMap); for (NameValuePair map : additionalMap) { log.info(map.getName() + ":" + map.getValue()); } HttpResponse httpResponse = null; try { // UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, RedirectUtil.SERVER_CHARSET); // httpGet.setEntity(entity); httpResponse = httpClient.execute(httpGet); if (httpResponse != null) { HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null) { log.info(responseEntity.toString()); responseEntity.writeTo(response.getOutputStream()); } } } catch (IOException e) { e.printStackTrace(); } if (httpResponse != null) { response.setStatus(httpResponse.getStatusLine().getStatusCode()); //logger.info(httpResponse.toString()); HeaderIterator headerIterator = httpResponse.headerIterator(); while (headerIterator.hasNext()) { Header header = headerIterator.nextHeader(); if (header.getName().equals("Content-Type")) { //response.addHeader(header.getName(), header.getValue()); response.setHeader(header.getName(), header.getValue());//或許可以解決重定向亂碼(好像沒影響) } } response.setHeader("Server", "nginx"); } } public static void httpPost(String url, HttpServletRequest request, HttpServletResponse response, List<NameValuePair> additionalMap) { CloseableHttpClient httpClient = SSLUtils.createSSLClientDefault(); HttpPost httpPost = new HttpPost(RedirectUtil.getHost(url)); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); httpPost.addHeader(headerName, request.getHeader(headerName)); } List<NameValuePair> params = new ArrayList<NameValuePair>(); Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = parameterNames.nextElement(); for (String value : request.getParameterValues(parameterName)) { if (value!=null&&!value.equals("null")) params.add(new BasicNameValuePair(parameterName, value)); } } params.addAll(additionalMap); for (NameValuePair map : additionalMap) { log.info(map.getName() + ":" + map.getValue()); } HttpResponse httpResponse = null; try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, RedirectUtil.SERVER_CHARSET); httpPost.setEntity(entity); httpResponse = httpClient.execute(httpPost); if (httpResponse != null) { HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null) { log.info(responseEntity.toString()); responseEntity.writeTo(response.getOutputStream()); } } } catch (IOException e) { e.printStackTrace(); } if (httpResponse != null) { response.setStatus(httpResponse.getStatusLine().getStatusCode()); //logger.info(httpResponse.toString()); HeaderIterator headerIterator = httpResponse.headerIterator(); while (headerIterator.hasNext()) { Header header = headerIterator.nextHeader(); if (header.getName().equals("Content-Type")) { //response.addHeader(header.getName(), header.getValue()); response.setHeader(header.getName(), header.getValue());//或許可以解決重定向亂碼(好像沒影響) } } response.setHeader("Server", "nginx"); } } public static void httpPut(String url, HttpServletRequest request, HttpServletResponse response, List<NameValuePair> additionalMap) { CloseableHttpClient httpClient = SSLUtils.createSSLClientDefault(); HttpPut httpPut = new HttpPut(RedirectUtil.getHost(url)); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); httpPut.addHeader(headerName, request.getHeader(headerName)); } List<NameValuePair> params = new ArrayList<NameValuePair>(); Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = parameterNames.nextElement(); for (String value : request.getParameterValues(parameterName)) { if (value!=null&&!value.equals("null")) params.add(new BasicNameValuePair(parameterName, value)); } } params.addAll(additionalMap); for (NameValuePair map : additionalMap) { log.info(map.getName() + ":" + map.getValue()); } HttpResponse httpResponse = null; try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, RedirectUtil.SERVER_CHARSET); httpPut.setEntity(entity); httpResponse = httpClient.execute(httpPut); if (httpResponse != null) { HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null) { log.info(responseEntity.toString()); responseEntity.writeTo(response.getOutputStream()); } } } catch (IOException e) { e.printStackTrace(); } if (httpResponse != null) { response.setStatus(httpResponse.getStatusLine().getStatusCode()); //logger.info(httpResponse.toString()); HeaderIterator headerIterator = httpResponse.headerIterator(); while (headerIterator.hasNext()) { Header header = headerIterator.nextHeader(); if (header.getName().equals("Content-Type")) { //response.addHeader(header.getName(), header.getValue()); response.setHeader(header.getName(), header.getValue());//或許可以解決重定向亂碼(好像沒影響) } } response.setHeader("Server", "nginx"); } } public static void httpDelete(String url, HttpServletRequest request, HttpServletResponse response, List<NameValuePair> additionalMap) { String method = request.getMethod(); CloseableHttpClient httpClient = SSLUtils.createSSLClientDefault(); HttpDelete httpDelete = new HttpDelete(RedirectUtil.getHost(url)); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); httpDelete.addHeader(headerName, request.getHeader(headerName)); } List<NameValuePair> params = new ArrayList<NameValuePair>(); Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = parameterNames.nextElement(); for (String value : request.getParameterValues(parameterName)) { if (value!=null&&!value.equals("null")) params.add(new BasicNameValuePair(parameterName, value)); } } params.addAll(additionalMap); for (NameValuePair map : additionalMap) { log.info(map.getName() + ":" + map.getValue()); } HttpResponse httpResponse = null; try { // UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, RedirectUtil.SERVER_CHARSET); // httpDelete.setEntity(entity); httpResponse = httpClient.execute(httpDelete); if (httpResponse != null) { HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null) { log.info(responseEntity.toString()); responseEntity.writeTo(response.getOutputStream()); } } } catch (IOException e) { e.printStackTrace(); } if (httpResponse != null) { response.setStatus(httpResponse.getStatusLine().getStatusCode()); //logger.info(httpResponse.toString()); HeaderIterator headerIterator = httpResponse.headerIterator(); while (headerIterator.hasNext()) { Header header = headerIterator.nextHeader(); if (header.getName().equals("Content-Type")) { //response.addHeader(header.getName(), header.getValue()); response.setHeader(header.getName(), header.getValue());//或許可以解決重定向亂碼(好像沒影響) } } response.setHeader("Server", "nginx"); } } public static String httpRequest(String hostPrefix, String url, List<NameValuePair> params) { CloseableHttpClient httpClient = SSLUtils.createSSLClientDefault(); log.info(url); HttpPost httpPost = new HttpPost(RedirectUtil.getHost(hostPrefix, url)); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, RedirectUtil.SERVER_CHARSET); httpPost.setEntity(entity); HttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse != null) { HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null) { String result = EntityUtils.toString(responseEntity, RedirectUtil.SERVER_CHARSET); log.info(result); return result; } } } catch (IOException e) { e.printStackTrace(); } return null; } /** * 使用httpclint 傳送檔案 * @author: qingfeng * @date: 2019-05-27 * @param file * 上傳的檔案 * @return 響應結果 */ public static void uploadFile(String url , MultipartFile file, String fileParamName,HttpServletRequest request) { try { RestTemplateUtils restTemplateUtils = new RestTemplateUtils(); ByteArrayResource resource = new MultipartFileResource(file); MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add(fileParamName, resource); log.info("請求採集服務url:{}",url); restTemplateUtils.sendPostFile(RedirectUtil.getHost(url),param,request); }catch (Exception e){ e.printStackTrace(); } } }