《HttpClient官方文档》1.1 执行请求(一)

    xiaoxiao2024-01-25  171

    1.1. 执行请求

    HttpClient最基本的功能就是执行HTTP方法。 一个HTTP方法的执行包含一次或多次HTTP请求与响应,通常由HttpClient的内部处理。 用户提供一个请求对象,HttpClient发送该请求到目标服务器,服务器返回相应的响应对象,如果执行未成功则抛出一个异常。

    很自然地,HttpClient的API的主要入口点就是定义了上述协议的HttpClient接口。下面是一个最简单的请求执行过程例子

    CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://localhost/"); CloseableHttpResponse response = httpclient.execute(httpget); try { <...> } finally { response.close(); }

    1.1.1. HTTP 请求

    所有HTTP请求都有由方法名,请求URI和HTTP协议版本组成的请求行。

    HttpClient支持开箱即用HTTP/1.1规范中定义的所有HTTP方法:GET, HEAD,POST, PUT, DELETE,TRACE and OPTIONS。它们都有一个特定的类对应这些方法类型: HttpGet,HttpHead, HttpPost,HttpPut, HttpDelete,HttpTrace, and HttpOptions.

    请求的URI是统一资源定位符,它标识了应用于哪个请求之上的资源。HTTP请求的URI包含协议方案,主机名,可选的端口,资源路径,可选查询和可选片段。

    HttpGet httpget = new HttpGet( "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");

    HttpClient 提供 URIBuilder 实用类来简化请求 URL的创建和修改.

    URI uri = new URIBuilder() .setScheme("http") .setHost("www.google.com") .setPath("/search") .setParameter("q", "httpclient") .setParameter("btnG", "Google Search") .setParameter("aq", "f") .setParameter("oq", "") .build(); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());

    输出内容为 >

    http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=

    1.1.2. HTTP 响应

    HTTP响应是服务器端在接收和解释客户端请求消息后,返回客户端的消息。该消息的第一行包含协议版本以及后面跟着的数字形式的状态代码和相关的文本段。

    HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); System.out.println(response.getProtocolVersion()); System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString());

    输出内容为 >

    HTTP/1.1 200 OK HTTP/1.1 200 OK

    1.1.3. 消息头

    HTTP消息可以包含多个描述该消息属性的头部诸如内容长度,内容类型等,HttpClient的提供方法来检索,添加,删除和枚举这些头部。

    HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); Header h1 = response.getFirstHeader("Set-Cookie"); System.out.println(h1); Header h2 = response.getLastHeader("Set-Cookie"); System.out.println(h2); Header[] hs = response.getHeaders("Set-Cookie"); System.out.println(hs.length);

    输出内容为 >

    Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost" 2

    获得所有头部给定类型的最有效的方法是使用HeaderIterator 接口.

    HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderIterator it = response.headerIterator("Set-Cookie"); while (it.hasNext()) { System.out.println(it.next()); }

    输出内容为 >

    Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost"

    它还提供了方便的方法来解析HTTP消息成为独立头部元素。

    HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator("Set-Cookie")); while (it.hasNext()) { HeaderElement elem = it.nextElement(); System.out.println(elem.getName() + " = " + elem.getValue()); NameValuePair[] params = elem.getParameters(); for (int i = 0; i < params.length; i++) { System.out.println(" " + params[i]); } }

    输出内容为 >

    c1 = a path=/ domain=localhost c2 = b path=/ c3 = c domain=localhost

    转载自 并发编程网 - ifeve.com

    最新回复(0)