HttpURLConnection 实现代理。
2015-11-03 11:53
501 查看
public class ForwardProxyController extends AbstractController { private String sourceContextPath; private String targetContextHostUrl; @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws IOException { Logger.info(this, "request : " + request.getPathInfo()); try { HttpURLConnection connection = parse(request); response.setContentType(connection.getHeaderField("Content-Type")); IOUtils.copy(connection.getInputStream(), response.getOutputStream()); }catch (Exception e) { response.getOutputStream().write("".getBytes()); } return null; } private HttpURLConnection parse(HttpServletRequest request) throws IOException { Logger.info(this, "request : " + request.getRequestURI()); String requestString = request.getRequestURI().replace(sourceContextPath, ""); String queryString = request.getQueryString(); String uri = targetContextHostUrl + requestString; if(StringUtils.isNotBlank(queryString)){ uri = uri + "?" + queryString; } Logger.info(this, "target uri : " + uri); HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection(); connection.setDoOutput(true); connection.setRequestMethod(request.getMethod()); connection.setRequestProperty("Content-type", request.getContentType()); connection.setRequestProperty("contentType", "utf-8"); IOUtils.copy(request.getInputStream(), connection.getOutputStream()); return connection; } }
<bean id="reverseProxyController" class="com.lufax.operation.gw.ForwardProxyController" > <property name="sourceContextPath" value="${OPERATION_GW_CONTENT_PATH}" /> <property name="targetContextHostUrl" value="${OPERATION_APP_HOST_URL}${OPERATION_APP_CONTENT_PATH}" /> </bean> <bean id="forwardProxyController" class="com.lufax.operation.gw.ForwardProxyController" > <property name="sourceContextPath" value="${OPERATION_GW_CONTENT_PATH}" /> <property name="targetContextHostUrl" value="${B_SYSTEM_HOST_URL}" /> </bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /bsfront/**=forwardProxyController /service/**=reverseProxyController </value> </property> <property name="order" value="10000" /> </bean>
web.xml
<servlet> <servlet-name>dynamic-velocity</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/operation-gw-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dynamic-velocity</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
相关文章推荐
- ios 网络连接判断
- html5 XMLHttpRequest 头像上传带进度
- 通过网络图片地址来访问图片的实现方法!
- Deep Convolutional Network Cascade for Facial Point Detection阅读笔记
- virtualbox中xp系统本地连接网络电缆被拔出
- 浅论Android网络请求库——android-async-http
- HTTP接口安全
- Java实现Http服务器(三)
- python发送HTTP请求
- Android网络编程五:(7)Volley之ImageCache
- TCP/IP详解 卷1 笔记 第17章 TCP:传输控制协议
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- 查看网络端口情况
- 数据业务建立流程之发起网络连接过程(原)
- TCP长链接调试利器nc
- 【转】tcp 出现rst情况整理
- spark深度学习算法(CNN卷积神经网络)的测试与分析
- java通过ping 判断网络是否正常
- 使用CInternetSession和CHttpFile提取网页源码
- iOS开发 - AFNetworking网络请求