用POST方式 重定向

转载自: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.
展开阅读全文

没有更多推荐了,返回首页