使用HTTPCLIENT进行带参数跨域跳转

1. 首先在工程中加入commons-httpclient-3.0.jar

2. 应用方法实例:

public ModelAndView dispatchPage(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ClassNotFoundException{
		
                // 创建 HttpClient 的实例
                HttpClient httpClient = new HttpClient();
                //获得跳转地址
		String url = "http://192.168.23.13/XXXX/Y.do";
		PostMethod postMethod = new PostMethod(url);
		// 获取参数值并传入 
		postMethod.addParameter("KEY", "VALUE");
                postMethod.addParameter("KEY", "VALUE");

		// 执行postMethod
		int statusCode = httpClient.executeMethod(postMethod);
		
		//输出回应
		InputStream is = postMethod.getResponseBodyAsStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is,
			    "utf-8"));
		String line = "";
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
		//查看缓存
		Cookie[] cookies = httpClient.getState().getCookies();
		System.out.println("cookies length = " + cookies.length + "    state="  
	               + statusCode);                                                       
		for (int i = 0; i < cookies.length && cookies.length!=0; i++) {                              
			System.out.println("cookie - " + i + ":");                          
			System.out.println(cookies.toString());                             
			System.out.println(cookies[i].getDomain());                         
			System.out.println(cookies[i].getName());                           
			System.out.println(cookies[i].getPath());                           
			System.out.println(cookies[i].getValue());                          
			System.out.println("---------------------");                        
		}
		int a = HttpStatus.SC_OK;
               
		Map m = new HashMap();
		// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
		// 301或者302
		if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
			    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
			Header header = postMethod.getResponseHeader("location");
			String location = null;
			if (header != null) {
				location = header.getValue();
				System.out.println("aaaaaaaa" + location);
				if((location == null) || location.equals("")){
					location = "/";
				}
				GetMethod redirect = new GetMethod(location);
				httpClient.executeMethod(redirect);
				redirect.releaseConnection();
                                //存放最终跳转的链接
				m.put("dispatchUrl", location);
			}else{
				//System.out.println("bbbbbbbbbbbbb");
			}
			postMethod.releaseConnection();
			
		}
		return new ModelAndView("dispatchPage",m);
	}

3. 页面dispatchPage跳转写法

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/taglibs.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>页面跳转中</title>
<script language="javascript" type="text/javascript">
window.location.href="${dispatchUrl}"; 
</script>
</head>
<body>
页面正在跳转中。。。请稍候。。。
</body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值