配色: 字号:
重定向Http status code 303 和 302
2016-10-10 | 阅:  转:  |  分享 
  
重定向Httpstatuscode303和302

http302

http303

Http302

302是一个普通的重定向代码。直观的看来是,请求者(浏览器或者模拟http请求)发起一个请求,然后服务端重定向到另一个地址。而事实上,服务端仅仅是增加一条属性到header,location=重定向地址。而一般的,浏览器会自动的再去请求这个location,重新获取资源。也就是说,这个会使得浏览器发起两次请求。



Example



Clientrequest:



GET/index.htmlHTTP/1.1

Host:www.example.com



Serverresponse:



HTTP/1.1302Found

Location:http://www.iana.org/domains/example/



实验

首先,我们用一个Map来存储信息,key为username,value为随机数。

当我请求list的时候,跳转到users,来获取所有的用户。



Mapusers=newHashMap<>();



@RequestMapping(value="/list",method=RequestMethod.GET)

publicStringindex(){

return"redirect:/users";

}



@ResponseBody

@RequestMapping(value="/users",method=RequestMethod.GET)

publicResponseEntitygetUsers(){

ResponseEntityresponseEntity=newResponseEntity(users,HttpStatus.OK);

returnresponseEntity;

}

当时用浏览器访问的时候,会明显的看到浏览器地址变了,也就是说我明明请求的是list,结果你给我变成了users。然而,由于浏览器帮我们做了跳转的工作,我们感觉不出来,但从地址栏还是可以看到的。



查看

通过拦截请求可以看出来,访问了两次:





并且list是302,而users是200.也就是说list进行了重定向。再来看list的response:



RequestURL:https://localhost:8443/list

RequestMethod:GET

StatusCode:302

RemoteAddress:127.0.0.1:8888



ResponseHeaders

viewsource

Cache-Control:no-cache,no-store,max-age=0,must-revalidate

Content-Language:zh-CN

Content-Length:0

Date:Thu,08Sep201614:31:33GMT

Expires:0

Location:https://localhost:8443/users

Pragma:no-cache

Strict-Transport-Security:max-age=31536000;includeSubDomains

X-Application-Context:application:dev:8443

X-Content-Type-Options:nosniff

X-Frame-Options:DENY

X-XSS-Protection:1;mode=block



RequestHeaders

viewsource

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8

Accept-Encoding:gzip,deflate,sdch,br

Accept-Language:zh-CN,zh;q=0.8

Authorization:BasicYWRtaW46dGVzdA==

Connection:keep-alive

Host:localhost:8443

Upgrade-Insecure-Requests:1

User-Agent:Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/52.0.2743.82Safari/537.36

最关键的就是location:



Location:https://localhost:8443/users

浏览器获取到这个资源定位后就GET访问获取。所以users的请求是这样的:



RequestURL:https://localhost:8443/users

RequestMethod:GET

StatusCode:200

RemoteAddress:127.0.0.1:8888



ResponseHeaders

viewsource

Cache-Control:no-cache,no-store,max-age=0,must-revalidate

Content-Type:application/json;charset=UTF-8

Date:Thu,08Sep201614:31:33GMT

Expires:0

Pragma:no-cache

Strict-Transport-Security:max-age=31536000;includeSubDomains

Transfer-Encoding:chunked

X-Application-Context:application:dev:8443

X-Content-Type-Options:nosniff

X-Frame-Options:DENY

X-XSS-Protection:1;mode=block



RequestHeaders

viewsource

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8

Accept-Encoding:gzip,deflate,sdch,br

Accept-Language:zh-CN,zh;q=0.8

Authorization:BasicYWRtaW46dGVzdA==

Connection:keep-alive

Host:localhost:8443

Upgrade-Insecure-Requests:1

User-Agent:Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/52.0.2743.82Safari/537.36

redirect的另一个作用是原请求的内容将会被舍弃,即如果是post请求,redirect的时候默认是不带参数的。与之相对应的forward的请求是转发,只有一次请求,并且带body转发过去。



Http303

303SeeOther。通常是指所请求的资源在别的地方,并且同302一样,会在header中的location标明资源的位置。在我的一个是使用过程中,我想要创建一个user,当关于这个user的key已经存在的时候,server将返回303,并且告之这个user的获取位置。



Example

Clientrequest:



POST/HTTP/1.1

Host:www.example.com



Serverresponse:



HTTP/1.1303SeeOther

Location:http://example.org/other



实验

我将要发送post请求创建user,如果user已经存在则返回303





Mapusers=newHashMap<>();



@ResponseBody

@RequestMapping(value="/users",method=RequestMethod.POST)

publicResponseEntitycreateUser(Stringusername){

DoubleluckNum=users.get(username);

if(luckNum==null){

doublerandom=Math.random();

users.put(username,random);

returnnewResponseEntity(random,HttpStatus.OK);

}else{

MultiValueMapheaders=newHttpHeaders();

headers.add("Location","/users/"+username);

returnnewResponseEntity(luckNum,headers,HttpStatus.SEE_OTHER);

}

}



@ResponseBody

@RequestMapping(value="/users/{username}",method=RequestMethod.GET)

publicResponseEntitygetUser(@PathVariable("username")Stringusername){

ResponseEntityresponseEntity=newww.baiyuewang.netResponseEntity("I''muser,Mynameis"+username+"Andmylucknumis"+users.get(username),HttpStatus.OK);

returnresponseEntity;

}

献花(0)
+1
(本文系thedust79首藏)