HTTP协议-Temporary Redirect(307)

Temporary Redirect(307):临时重定向。在HTTP1.1的规范:10.3.8 307 Temporary Redirect定义如下:

 

The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s) , since many pre-HTTP/1.1 user agents do not understand the 307 status. Therefore, the note SHOULD contain the information necessary for a user to repeat the original request on the new URI.

If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

 

废话很多,总的来说有意义的感觉就是:

  1. 临时重定向可以保持客户端原始URL不变,由服务器根据情况动态指定重定向地址。
  2. 整个操作需要两次请求,客户端第一次收到的是由服务器返回的一个空BODY的307响应,关键的在响应头中包含一个Location的指令,表明重新请求的URL,然后客户端自动重新请求新的地址。
  3. 代理的一些处理规则,I DON'T CARE.

 

下面是用TOMCAT+SERVLET做的一个DEMO。

节约篇幅,无package和import,请自己Ctrl+Shift+O

 

/**
 * HTTP 协议,Temporary Redirect(307) 测试
 * 
 * @author zhangpu
 * 
 */
public class Http307TestServlet extends HttpServlet {

	private static final long serialVersionUID = 7047368191379656914L;
	static final String tempRedirectUrl = "http://www.baidu.com";

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setStatus(307);
		resp.setHeader("Location", tempRedirectUrl);
		PrintWriter p = resp.getWriter();
		p.println("Location:" + tempRedirectUrl);
		p.println("Note: this is 307 redirect Url.");
		p.close();
	}
}

 

web.xml中加入Servlet的定义:

 

	<servlet>
		<servlet-name>temporary redirect test</servlet-name>
		<servlet-class>org.acooly.http.Http307TestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>temporary redirect test</servlet-name>
		<url-pattern>/http/307</url-pattern>
	</servlet-mapping>

 

启动TOMCAT,浏览器中输入:http://localhost:8080/http/307

OW,直接开始百度。

 

 

 

 

 

你可能感兴趣的:(temporary)