用POST方式 重定向

67 篇文章 0 订阅

转载自:http://blog.csdn.net/ku1989/article/details/8254038

用POST方式 重定向

在项目里,如果发生异常,我会需要重定向到一个指定的页面去告诉别人出问题了。这个时候一般我们都是用response.sendRedirect(url?mesage=xxxx);这是GET方式的。如果我们要以POST方式重定向时,找了一下,发现没有现成的东西,可以办得到。这里要自己写一个方法去实现:

httpClient.java

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
/**

* @author billtsang
*
*/
public class HttpClient {
Map<String, String> parameter=new HashMap<String, String>();
HttpServletResponse response;

public HttpClient(HttpServletResponse response)
{
   this.response=response;
}
public void setParameter(String key,String value)
{
   this.parameter.put(key, value);
}
public void sendByPost(String url) throws IOException
{
   this.response.setContentType("text/html");
   PrintWriter out = this.response.getWriter();
   out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
   out.println("<HTML>");
   out.println(" <HEAD><TITLE>sender</TITLE></HEAD>");
   out.println(" <BODY>");
   out.println("<form name=\"submitForm\" action=\""+url+"\" method=\"post\">");
     Iterator<String> it=this.parameter.keySet().iterator();
   while(it.hasNext())
   {
    String key=it.next();
    out.println("<input type=\"hidden\" name=\""+key+"\" value=\""+this.parameter.get(key)+"\"/>");
   }
   out.println("</from>");
   out.println("<script>window.document.submitForm.submit();</script> ");
   out.println(" </BODY>");
   out.println("</HTML>");
   out.flush();
   out.close();

}
}

在我们要跳转地方

HttpClient http=new HttpClient (response);

http.setParameter("message","xxxx");

http.sendByPost(url);




---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Redirecting a post
For redirecting previously sent post data to another url: use an HTTP 307 (Temporary Redirect) header. The browser will resend the original data to the other url.

I don't know much about ASP.Net, but In PHP, it would be done with something similar to this:

PHP Code:
header('HTTP/1.0 307 Temporary Redirect',$replace=true,307);
header('Location: some_new_url_here'); 
This doesn't allow you to modify the original data, so it is not the solution for David. Additionally, the browser may ask the user to authorize the repost to another location (for instance, Firefox 3 does it).

Posting new data to another URL
If you want the browser to post the newly generated stuff to another URL, you can create a new form with hidden fields. Just keep in mind that some browsers might have javascript deactivated/not supported (does that still exist?), so you should make shure that your page provides relevant information and ask the user to push the submit button in the eventuality javascript submission fails.

As far as I know, there is no other way to force the client to post new data.

Server2server transfer of data
A good solution would be to post the data to the other server using a server-side HTTP request BEFORE you actually redirect the client to the new page. Of course, this is only possible if you control the scripts on the other server.

The first server can then then send the data directly to the second server, which can just stash the data somewhere and return a unique id string in the response (some random hash) that you would need to put in your redirection link when you redirect the client (eg. some_url?id=8aef38abd293). The second server would use this id to retrieve the previously stashed data and do it's business with it.

Proxying the second server
If you don't have control over the second server, you can make a server-side post HTTP request to the second server, and send (or interpret) it's response back to the client without redirecting the client at all.

Note that doing server-side requests and redirecting the client are two very distinct operations.
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值