HttpClient介绍

来源:互联网 发布:美工钢笔怎么写字 编辑:程序博客网 时间:2024/05/02 00:08

产生背景

HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

功能

(1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)(2)支持自动转向(3)支持 HTTPS 协议(4)支持代理服务器等

使用场景

当一个项目中要想要直接通过访问Http协议来请求想要的资源时,可以使用httpClient,它将访问http协议的一系列的操作进行了封装,可以让我们很方便的进行访问。目前最流行的属移动端了。通常app只用来进行显示,真正的业务逻辑单独放在一个服务端,单独作为一个服务供app进行调用。这样只需要暴露服务层中controller的uri地址即可。

使用流程

使用 HttpClient 需要以下 6 个步骤:1 创建 HttpClient 的实例2.创建某种连接方法的实例,在这里是GetMethod。在 GetMethod 的构造函数中传入待连接的地址3、调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例4、 读 response5、释放连接。无论执行方法是否成功,都必须释放连接6、 对得到后的内容进行处理

入门实例

GET

    @Test    public void doGet() throws ClientProtocolException, IOException{        //创建一个httpClient对象        CloseableHttpClient httpClient=HttpClients.createDefault();        //创建一个get对象        HttpGet get=new HttpGet("http://www.baidu.com");        //执行请求        CloseableHttpResponse response=httpClient.execute(get);        //获取响应的结果        int statusCode=response.getStatusLine().getStatusCode();        System.out.println("状态码为"+statusCode);        HttpEntity entity=response.getEntity();        //将entity中的内容读入到一个字符串中        String string =EntityUtils.toString(entity,"utf-8");        System.out.print(string);        //关闭httpClient对象        response.close();        httpClient.close();    }    @Test    public void duGetWithParam() throws URISyntaxException, ClientProtocolException, IOException{        //创建httpClient对象        CloseableHttpClient httpClient=HttpClients.createDefault();        //通过uriBuilder来构建查询的连接和参数        URIBuilder uriBuilder=new URIBuilder("http://www.sogou.com/web");        uriBuilder.addParameter("query", "花千骨");        //创建get对象        HttpGet get=new HttpGet(uriBuilder.build());        //执行请求        CloseableHttpResponse response=httpClient.execute(get);        //获取响应的结果        int statusCode=response.getStatusLine().getStatusCode();        System.out.println("状态码为"+statusCode);        HttpEntity entity=response.getEntity();        //将entity中的内容读入到一个字符串中        String string =EntityUtils.toString(entity,"utf-8");        System.out.print(string);        //关闭httpClient对象        response.close();        httpClient.close();    }

POST

@Test    public void testPost() throws ClientProtocolException, IOException{        //创建httpClient对象        CloseableHttpClient httpClient=HttpClients.createDefault();        //创建一个post对象        HttpPost post=new HttpPost("http://localhost:8080/taotao-portal/httpClient/post.html");        //执行请求        CloseableHttpResponse response=httpClient.execute(post);        //将entity中的内容读入到一个字符串中        String string =EntityUtils.toString(response.getEntity(),"utf-8");        System.out.print(string);    }    @Test    public void testPostWithParam() throws ClientProtocolException, IOException{        //创建httpClient对象        CloseableHttpClient httpClient=HttpClients.createDefault();        //创建一个post对象        HttpPost post=new HttpPost("http://localhost:8080/taotao-portal/httpClientparam/post.html");        List<NameValuePair> kvList=new ArrayList<NameValuePair>();        kvList.add(new BasicNameValuePair("username","zhangsan"));        kvList.add(new BasicNameValuePair("password", "123"));        //包装成一个entity对象        StringEntity entity=new UrlEncodedFormEntity(kvList);        post.setEntity(entity);        //执行请求        CloseableHttpResponse response=httpClient.execute(post);        //将entity中的内容读入到一个字符串中        String string =EntityUtils.toString(response.getEntity(),"utf-8");        System.out.print(string);        response.close();        httpClient.close();    }
1 0