当前位置: 首页 > news >正文

重定向Http status code 303 和 302

  1. http 302
  2. http 303

Http 302

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

Example

Client request:

GET /index.html HTTP/1.1
Host: www.example.com

Server response:

HTTP/1.1 302 Found
Location: http://www.iana.org/domains/example/

实验

  1. 首先,我们用一个Map来存储信息,key为username,value为随机数。
  2. 当我请求list的时候,跳转到users,来获取所有的用户。

Map<String, Double> users = new HashMap<>();

@RequestMapping(value = "/list", method = RequestMethod.GET)
public String index(){
    return "redirect:/users";
}

@ResponseBody
@RequestMapping(value = "/users", method = RequestMethod.GET)
public ResponseEntity getUsers(){
    ResponseEntity responseEntity = new ResponseEntity(users, HttpStatus.OK);
    return responseEntity;
}   

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

查看
通过拦截请求可以看出来,访问了两次:
686418-20160908223534363-1357573526.png

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

Request URL:https://localhost:8443/list
Request Method:GET
Status Code:302 
Remote Address:127.0.0.1:8888

Response Headers
view source
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Language:zh-CN
Content-Length:0
Date:Thu, 08 Sep 2016 14:31:33 GMT
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

Request Headers
view source
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:Basic YWRtaW46dGVzdA==
Connection:keep-alive
Host:localhost:8443
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36

最关键的就是location:

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

Request URL:https://localhost:8443/users
Request Method:GET
Status Code:200 
Remote Address:127.0.0.1:8888

**Response Headers**
view source
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json;charset=UTF-8
Date:Thu, 08 Sep 2016 14:31:33 GMT
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

**Request Headers**
view source
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:Basic YWRtaW46dGVzdA==
Connection:keep-alive
Host:localhost:8443
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36

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


Http 303

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

Example
Client request:

POST / HTTP/1.1
Host: www.example.com

Server response:

HTTP/1.1 303 See Other
Location: http://example.org/other

实验

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


    Map<String, Double> users = new HashMap<>();

    @ResponseBody
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public ResponseEntity createUser(String username){
        Double luckNum = users.get(username);
        if (luckNum ==null){
            double random = Math.random();
            users.put(username, random);
            return new ResponseEntity(random,HttpStatus.OK);
        }else{
            MultiValueMap<String,String> headers = new HttpHeaders();
            headers.add("Location", "/users/"+username);
            return new ResponseEntity(luckNum, headers, HttpStatus.SEE_OTHER);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/users/{username}", method = RequestMethod.GET)
    public ResponseEntity getUser(@PathVariable("username") String username){
        ResponseEntity responseEntity = new ResponseEntity("I'm user, My name is  "+ username+ " And my luck num is "+users.get(username), HttpStatus.OK);
        return responseEntity;
    }

发送
686418-20160908230930207-909758310.png

查看拦截
686418-20160908230935926-1218749076.png

可以看到,post的时候返回303,并且在返回的response的header中添加了:

Location: /users/test

所以see other的意思就是去别的地方看看。值得注意的是,如果返回303,但是没有添加location,那么只会查看一条请求303.而在httpclient的默认处理中,这时候会抛出exception:location not found。

参考

  • 维基百科 http 302
  • 维基百科 http 303

相关文章:

  • 输入一组数组,回车结束
  • Java反射在JVM的实现
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • Android IOS WebRTC 音视频开发总结(八十五)-- 使用WebRTC广播网络摄像头视频(下)...
  • jdbc conn.commit()提交事务和 rollback()使用
  • 每天一个linux命令:mkdir命令
  • 程序(进程)内存分布解析【转】
  • jenkins updatecenter更新插件有问题
  • Django+uwsgi+Nginx安装部署
  • 基于人脸识别的商业大数据4
  • 用xargs批量删除Redis的keys的技巧
  • php中的邮件技术
  • 一起参Ember.js讨论、问答社区。
  • 初学Redis(1)——认识Redis
  • 移动端流体布局
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • CSS相对定位
  • ES学习笔记(12)--Symbol
  • express.js的介绍及使用
  • JAVA并发编程--1.基础概念
  • React Native移动开发实战-3-实现页面间的数据传递
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • springboot_database项目介绍
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 从零开始的无人驾驶 1
  • 分享自己折腾多时的一套 vue 组件 --we-vue
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 移动端解决方案学习记录
  • 用Python写一份独特的元宵节祝福
  • 优秀架构师必须掌握的架构思维
  • 运行时添加log4j2的appender
  • 阿里云ACE认证之理解CDN技术
  • #Linux(帮助手册)
  • #pragma预处理命令
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (三)模仿学习-Action数据的模仿
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • (转)详解PHP处理密码的几种方式
  • .Mobi域名介绍
  • .net 8 发布了,试下微软最近强推的MAUI
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .net MVC中使用angularJs刷新页面数据列表
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET成年了,然后呢?
  • .net访问oracle数据库性能问题
  • /etc/shadow字段详解
  • @德人合科技——天锐绿盾 | 图纸加密软件有哪些功能呢?
  • [383] 赎金信 js
  • [Android] Upload package to device fails #2720
  • [asp.net core]project.json(2)
  • [BUG] Authentication Error
  • [bzoj1901]: Zju2112 Dynamic Rankings
  • [BZOJ4337][BJOI2015]树的同构(树的最小表示法)
  • [C++提高编程](三):STL初识