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。然而,由于浏览器帮我们做了跳转的工作,我们感觉不出来,但从地址栏还是可以看到的。

查看

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

并且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;
}

发送

查看拦截

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

Location: /users/test

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

参考

重定向Http status code 303 和 302的更多相关文章

  1. 详解重定向(HTTP状态码301/302/303/307/408)附例子

    本文为原创文章,转载请注明出处. 今天打算好好把状态码301.302.303.307.308好好撸一遍,并会测试下一些例子. 状态码的解释 我们都知道重定向与这几种状态码有关,来看下这几种HTTP状态 ...

  2. http status code

    属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...

  3. 常见http status code

    常见http status code 常见的状态码: HTTP: Status200– 服务器成功返回网页 HTTP: Status404– 请求的网页不存在 HTTP: Status503– 服务不 ...

  4. HTTP状态码(HTTP Status Code)【转】

    HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释:点击查看 1xx(临时响应 ...

  5. 500 status http status code 状态码

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Status Server error responses 500 Internal Server ...

  6. HTTP协议状态码详解(HTTP Status Code)(转)

    原文链接:HTTP协议状态码详解(HTTP Status Code) 使用ASP.NET/PHP/JSP 或者javascript都会用到http的不同状态,一些常见的状态码为: 200 – 服务器成 ...

  7. 常见的HTTP状态码(HTTP Status Code)

    HTTP状态码 当使用浏览器访问一个网页时,浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览 ...

  8. status http status code 状态码

    RFC 6585 - Additional HTTP Status Codes https://tools.ietf.org/html/rfc6585 https://developer.mozill ...

  9. HTTP Status Code [RFC]

    来源:http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml Hypertext Transfer Prot ...

随机推荐

  1. ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单

    前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...

  2. java中servlet的各种路径

    1. web.xml中<url-pattern>路径,(叫它Servlet路径!) > 要么以“*”开关,要么为“/”开头 2. 转发和包含路径 > *****以“/”开头:相 ...

  3. web全栈开发之网站开发二(弹出式登录注册框前端实现-类腾讯)

    这次给大家分享的是目前很多网站中流行的弹出式登录框,如下面的腾讯网登录界面,采用弹出式登录的好处是大大提升了网站的用户体验和交互性,用户不用重新跳转到指定的页面就能登录,非常方便 先来个演示地址 要实 ...

  4. MySQL中interactive_timeout和wait_timeout的区别

    在用mysql客户端对数据库进行操作时,打开终端窗口,如果一段时间没有操作,再次操作时,常常会报如下错误: ERROR (HY000): Lost connection to MySQL server ...

  5. 自定义搭建PHP开发环境

    学习了一段时间php了,因为之前是刚接触php,所以用的是集成安装包(wamp).现在想进一步了解apache.mysql.php之间的关系以及提升自己所以进行自定义搭建PHP开发环境.废话不多说,请 ...

  6. js从数组中随机取出不同的元素

    前言 上午处理个需求需要从一个总数组中随机取出不同的元素.共使用两个方法.第一种方法较常规,经测试有bug,数据量大以后随机几次返回的对象直接是function而不是object. 当然简单数据类型应 ...

  7. javascript中变量提升的理解

    网上找了两个经典的例子 var foo = 1; function bar() { if (!foo) { var foo = 10; } alert(foo); } bar(); // 10 var ...

  8. Angular2 Hello World 之 RC6

    angular2还没有发布正式版,确实有点不靠谱,变化太频繁,之前写的demo直接将js升级到最新版之后发现就不能用了……所以现在在写一篇demo——基于RC6.参考:http://web3.code ...

  9. 机器指令翻译成 JavaScript —— 终极目标

    上一篇,我们顺利将 6502 指令翻译成 C 代码,并演示了一个案例. 现在,我们来完成最后的目标 -- 转换成 JavaScript. 中间码输出 我们之所以选择 C,就是为了使用 LLVM.现在来 ...

  10. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...