第一章 深入Web请求过程

2018-03-16  本文已影响33人  01_小小鱼_01

随着Web 2.0的来临,互联网架构已经从传统的C/S架构转变为B/S(浏览器/服务器)架构。

从用户在浏览器输入www.taobao.com这个URL开始会经历那些过程的呢

如何发起请求
Linux命令

bogon:~ yudesong$ curl "http://item.taobao.com/item.htm?id=1264" > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   278  100   278    0     0   1976      0 --:--:-- --:--:-- --:--:--  1985

bogon:~ yudesong$ curl "https://detail.tmall.com/item.htm?id=35057524460&spm=a21bz.7725275.1998564545.1.5e2b551dwiAuE0&umpChannel=qianggou&u_channel=qianggou" -I
HTTP/1.1 302 Found
Server: Tengine
Date: Fri, 16 Mar 2018 14:59:14 GMT
Content-Type: text/html
Content-Length: 258
Connection: keep-alive
at_bucketid: sbucket_-1
X-Bucket-Id: -1
Location: https://login.taobao.com/jump?target=https%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d35057524460%26spm%3da21bz.7725275.1998564545.1.5e2b551dwiAuE0%26umpChannel%3dqianggou%26u_channel%3dqianggou%26tbpm%3d1
Via: cache3.cn19[,0]
Timing-Allow-Origin: *
EagleId: 7c5f9dcb15212123547974875e
Cache-Control: 

Java代码示例

public class GetSample{
  public static void main(String[] args) {
  //构造HttpClient的实例
  HttpClient httpClient = new HttpClient();
  //创建GET方法的实例
  GetMethod getMethod = new GetMethod("http://www.ibm.com");
  //使用系统提供的默认的恢复策略
  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler());
  try {
   //执行getMethod
   int statusCode = httpClient.executeMethod(getMethod);
   if (statusCode != HttpStatus.SC_OK) {
    System.err.println("Method failed: "
      + getMethod.getStatusLine());
   }
   //读取内容 
   byte[] responseBody = getMethod.getResponseBody();
   //处理内容
   System.out.println(new String(responseBody));
  } catch (HttpException e) {
   //发生致命的异常,可能是协议不对或者返回的内容有问题
   System.out.println("Please check your provided http address!");
   e.printStackTrace();
  } catch (IOException e) {
   //发生网络异常
   e.printStackTrace();
  } finally {
   //释放连接
   getMethod.releaseConnection();
  }
 }
}

HTTP解析
HTTP请求头

Accept-Charset    用于指定客户端接受的字符集
Accept-Encoding   可接受的内容编码
Accept-Language   自然语言 比如zh-cn
Host              指定被请求资源的Internet主机和端口号
User-Agent        客户端将它的操作系统、浏览器和其他属性告诉服务器
Connection        当前连接是否保持。

HTTP响应头:

Server            使用的服务器名称
Content-type      指明发送给接受者的实体正文的媒体类型,如Content_Type:text/html;charset = GBK
Content-Encoding: 告诉浏览器服务端采用的压缩编码
Content-Language: 描述了资源所用的自然语言
Content-Length  : 指明实体正文的长度
Keep-Alive      : 保持连接的时间

常见的状态码

404 请求资源不存在
200 客户端请求成功
400 客户端请求有语法错误
500 服务端发生不可预期的错误

浏览器的缓存设置
在浏览器端Ctrl+F5可以重新发起请求而不使用缓存数据。发出报文的请求头里面相关字段告诉服务器,不要缓存要最新的数据。
这两个字段是:Pragma:no-cache Cache-Control:no-cache

DNS域名解析过程

跟踪域名解析过程

[bogon:~ yudesong$ nslookup
> www.taobao.com
Server:     192.168.188.1
Address:    192.168.188.1#53

Non-authoritative answer:
www.taobao.com  canonical name = www.taobao.com.danuoyi.tbcache.com.
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 157.255.139.123
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 163.177.20.223

bogon:~ yudesong$ dig www.baidu.com +trace

; <<>> DiG 9.8.3-P1 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13933
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.         IN  A

;; ANSWER SECTION:
www.baidu.com.      509 IN  CNAME   www.a.shifen.com.
www.a.shifen.com.   182 IN  A   163.177.151.110
www.a.shifen.com.   182 IN  A   163.177.151.109

;; Query time: 10 msec
;; SERVER: 192.168.188.1#53(192.168.188.1)
;; WHEN: Fri Mar 16 23:10:30 2018
;; MSG SIZE  rcvd: 90

清除域名信息

Windows: ipconfig /flushdns
Linux:   /etc/init.d/nscd restart

几种域名的解析方式
域名解析记录主要分为:A 记录、MX记录、CNAME 记录、NS记录和 TXT记录

CDN
内容分发网络CDN

负载均衡
关于负载均衡的一切:总结与思考
负载均衡算法及手段
负载均衡集合
更多内容
1. 关于java dns cache (域名缓存时间)

上一篇 下一篇

猜你喜欢

热点阅读