HttpMessage包含客户端到服务端的请求以及服务端到客户端的响应,HttpRequest和HttpResponse接口均继承该接口。
根据RFC文档,一个http message 结构应该为
generic-message = start-line *(message-header CRLF) CRLF [ message-body ] start-line = Request-Line | Status-Line
注意:CRLF指回车换行
HttpRequest 是客户端到服务端请求的接口。
根据RFC文档,Request的结构应该为:
Request = Request-Line *(( general-header | request-header | entity-header ) CRLF) CRLF [ message-body ]
Request—Line的结构为:
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
注意: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 []}
HttpResponse 是服务端收到客户端的请求之后对客户端响应的消息的接口
根据RFC文档,Response的结构为:
Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ]
Status-Line 的结构为:
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
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 []}
一个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"] }
可以使用迭代器(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()); } }
还有便捷的方法将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("------------------------------------------"); } }
输出结果
c1==a------------------------------------------- path=/ domain=localhost------------------------------------------c2==b------------------------------------------- path=/------------------------------------------c3==c------------------------------------------- domain=localhost------------------------------------------image/png==null-------------------------------------------------------------------------------------
之后内容见下一篇博客,HttpCore 教程(二)
联系客服