打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
HttpCore 教程(一)

一、基础

(一) HttpMessage

HttpMessage包含客户端到服务端的请求以及服务端到客户端的响应,HttpRequest和HttpResponse接口均继承该接口。

根据RFC文档,一个http message 结构应该为

generic-message = start-line                  *(message-header CRLF)                  CRLF                  [ message-body ]     start-line = Request-Line | Status-Line
  • 1
  • 2
  • 3
  • 4
  • 5

注意:CRLF指回车换行

1、HttpRequest

HttpRequest 是客户端到服务端请求的接口。

根据RFC文档,Request的结构应该为:

Request = Request-Line          *(( general-header          | request-header          | entity-header ) CRLF)          CRLF          [ message-body ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Request—Line的结构为:

Request-Line = Method SP Request-URI SP HTTP-Version CRLF
  • 1

注意:SP指空格

最常用的HttpRequest的实现类为BasicHttpRequest

示例代码

public void httpRequestTest() {    HttpRequest httpRequest = new BasicHttpRequest("GET", "www.baidu.com", HttpVersion.HTTP_1_1);    System.out.println(httpRequest.getRequestLine());    //GET www.baidu.com HTTP/1.1    System.out.println(httpRequest);    //GET www.baidu.com []}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2、HttpResponse

HttpResponse 是服务端收到客户端的请求之后对客户端响应的消息的接口

根据RFC文档,Response的结构为:

Response = Status-Line           *(( general-header           | response-header           | entity-header ) CRLF)           CRLF           [ message-body ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Status-Line 的结构为:

Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
  • 1

HttpResponse 的实现类 BasicHttpResponse的使用

示例代码

public void httpResponseTest() {    HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");    System.out.println(httpResponse.getStatusLine());    //HTTP/1.1 200 OK    System.out.println(httpResponse);    //HTTP/1.1 200 OK []}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3、http message 的一些属性和方法

一个http message 包含许多描述Message属性的header(content length, content type, cookie, user-agent 等 )

http message 提供了一系列方法用来取出、添加、移除、枚举这些header

示例代码

public void httpMessageHeaderMethodTest() {        HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1,                                          HttpStatus.SC_OK,                                      "OK");        httpResponse.addHeader("Set-Cookie", "c1=a;path=/;domain=localhost");        httpResponse.addHeader("Set-Cookie","c2=b;path=\"/\",c3=c;domain=\"localhost\"");        Header header = new BasicHeader("Content-Type", "image/png");        httpResponse.addHeader(header);        Header h1 = httpResponse.getFirstHeader("Set-Cookie");        System.out.println(h1);        //Set-Cookie: c1=a;path=/;domain=localhost        Header h2 = httpResponse.getLastHeader("Set-Cookie");        System.out.println(h2);        //Set-Cookie: c2=b;path="/",c3=c;domain="localhost"        Header[] headers = httpResponse.getAllHeaders();        System.out.println(Arrays.toString(headers));        //[Set-Cookie: c1=a;path=/;domain=localhost, Set-Cookie: c2=b;path="/",c3=c;domain="localhost", Content-Type: image/png]        Header[] cookiesHeader = httpResponse.getHeaders("Set-Cookie");        System.out.println(Arrays.toString(cookiesHeader));        //[Set-Cookie: c1=a;path=/;domain=localhost, Set-Cookie: c2=b;path="/",c3=c;domain="localhost"]        httpResponse.removeHeaders("Content-Type");        System.out.println(Arrays.toString(httpResponse.getAllHeaders()));        //[Set-Cookie: c1=a;path=/;domain=localhost, Set-Cookie: c2=b;path="/",c3=c;domain="localhost"]    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

可以使用迭代器(HeaderIterator)来遍历给定名字的所有头

示例代码

public void headerIteratorTest() {        HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1,                                      HttpStatus.SC_OK,                                  "OK");        httpResponse.addHeader("Set-Cookie", "c1=a;path=/;domain=localhost");        httpResponse.addHeader("Set-Cookie","c2=b;path=\"/\",c3=c;domain=\"localhost\"");        HeaderIterator headerIterator1 = httpResponse.headerIterator("Set-Cookie");        while(headerIterator1.hasNext()) {            System.out.println(headerIterator1.next());        }        HeaderIterator headerIterator2 = httpResponse.headerIterator();        while(headerIterator2.hasNext()) {            System.out.println(headerIterator2.next());        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

还有便捷的方法将header解析成单独的header 元素(HeaderElementIterator、NameValuePair)

示例代码

public void headerElementIteratorTest() {        HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1,                                                  HttpStatus.SC_OK,                                                  "OK");        httpResponse.addHeader("Set-Cookie", "c1=a;path=/;domain=localhost");        httpResponse.addHeader("Set-Cookie","c2=b;path=\"/\",c3=c;domain=\"localhost\"");        Header header = new BasicHeader("Content-Type", "image/png");        httpResponse.addHeader(header);        HeaderElementIterator iterator = new BasicHeaderElementIterator(httpResponse.headerIterator());        while(iterator.hasNext()) {            HeaderElement headerElement = iterator.nextElement();            System.out.println(headerElement.getName()+"=="+headerElement.getValue());            System.out.println("-------------------------------------------");            NameValuePair[] params = headerElement.getParameters();            for(int i=0;i<params.length;i++) {                System.out.println("   "+params[i]);            }            System.out.println("------------------------------------------");        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

输出结果

c1==a-------------------------------------------   path=/   domain=localhost------------------------------------------c2==b-------------------------------------------   path=/------------------------------------------c3==c-------------------------------------------   domain=localhost------------------------------------------image/png==null-------------------------------------------------------------------------------------
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

之后内容见下一篇博客,HttpCore 教程(二) 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
HttpClient学习笔记
urllib详解
httpClient基础
Java网络通信之HttpClient
关于谷歌Chrom 80版本升级后,跨域Samesite必须有值影响跨域项目的解决
WEB网站常见受攻击方式及解决办法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服