java 跨域跳转页面_跨域请求页面跳转

packagebookstore;importjava.io.IOException;importjava.net.URLEncoder;importjava.util.Calendar;importjava.util.GregorianCalendar;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;import org.apache.commons.httpclient.*;importorg.apache.commons.httpclient.methods.GetMethod;importorg.apache.commons.httpclient.methods.PostMethod;/*** Servlet implementation class ShareWBAPI*/@WebServlet("/ShareWBAPI")public class ShareWBAPI extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/

publicShareWBAPI() {super();//TODO Auto-generated constructor stub

}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub

doPost(request,response);

}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub

String POST_URL="http://127.0.0.1:6080/helloworld/receiveWBAPI";//URL postUrl = new URL(POST_URL);//HttpURLConnection connection = (HttpURLConnection) postUrl//.openConnection();//connection.setDoOutput(true);//connection.setDoInput(true);//connection.setRequestMethod("POST");//connection.setUseCaches(false);//connection.setInstanceFollowRedirects(true);//connection.setRequestProperty("Content-Type",//"application/x-www-form-urlencoded");//

//connection.setRequestProperty(key, value)///**//*//* 与readContentFromPost()最大的不同,设置了块大小为5字节//*///connection.setChunkedStreamingMode(5);//connection.connect();//

///**//*//* 注意,下面的getOutputStream函数工作方式于在readContentFromPost()里面的不同//* 在readContentFromPost()里面该函数仍在准备http request,没有向服务器发送任何数据//* 而在这里由于设置了ChunkedStreamingMode,getOutputStream函数会根据connect之前的配置//* 生成http request头,先发送到服务器。//*///DataOutputStream out = new DataOutputStream(connection//.getOutputStream());//String content = "userId=11111&content=" + URLEncoder.encode("一个大肥人", "utf-8");//out.writeBytes(content);//

//out.flush();//out.close();

PostMethod postMethod= newPostMethod(POST_URL);

postMethod.addParameter("userId","1111");

postMethod.addParameter("content","主题");

HttpClient client= newHttpClient();

client.setConnectionTimeout(1000 * 60);int status=0;try{

status=client.executeMethod(postMethod);

}catch(HttpException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}if(status==HttpStatus.SC_OK) {

}else{

}

postMethod.releaseConnection();//因为Bjava,redirect所以返回302,

if ((status == HttpStatus.SC_MOVED_TEMPORARILY) ||(status == HttpStatus.SC_MOVED_PERMANENTLY) ||(status == HttpStatus.SC_SEE_OTHER) || (status ==HttpStatus.SC_TEMPORARY_REDIRECT)) {//读取新的URL地址

Header header = postMethod.getResponseHeader("location");if (header != null) {

String newuri=header.getValue();if ((newuri == null) || (newuri.equals("")))

newuri="/";

GetMethod redirect= newGetMethod(newuri);

client.executeMethod(redirect);Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+newuri); //跳转页面效果

System.out.println("Redirect:"+redirect.getStatusLine().toString());

redirect.releaseConnection(); }else System.out.println("Invalid redirect"); }

}

}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页
评论

打赏作者

weixin_39985279

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值