增加微信订阅号(在右侧),关注后,及时收到最新更新的文章。

Apache HttpComponet(4)–HttpClient4.3快速开始

HttpClient 智菲尔 3413℃ 0评论

1、安装

下载Jar包的方式,或者使用其它的包管理器方式,如maven引入方式。

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.6</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient-cache</artifactId>
  <version>4.3.6</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.3.6</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>fluent-hc</artifactId>
  <version>4.3.6</version>
</dependency>

2、HttpClient 4.3要求JDK版本 1.5+。

3、下面的代码演示HTTP GET/POST请求

//创建默认HttpClient
        CloseableHttpClient httpclient = HttpClients.createDefault();
        
        try {
            
            
            // ========== HTTP GET
            HttpGet httpGet = new HttpGet("http://www.sina.com.cn"); //必须加上http://协议名称
            //请求
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            
            try{
                System.out.println(response1.getStatusLine());
                HttpEntity entity1 = response1.getEntity();
                //业务代码
                System.out.println(entity1.getContentType());
                //消费entity
                EntityUtils.consume(entity1);
            }finally{
                //在response对象中还绑定着connect对象,允许用户直接使用socket来解析流,
                //为了正确处理本地资源,用户必须在finally块中调用CloseableHttpResponse#close()
                //注意,如果response内容未被完全消息掉的话,各其绑定的connection将不能安全被复用。
                //它将会被直接关闭
                response1.close();
            }
            
            // ============ HTTP POST
            String postUrl = "...";//指定post提交的地址
            HttpPost httpPost = new HttpPost();
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("username", "vip"));
            nvps.add(new BasicNameValuePair("password", "secret"));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            CloseableHttpResponse response2 = httpclient.execute(httpPost);
            try {
                System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                // 业务代码
                
                // 消息entity2
                EntityUtils.consume(entity2);
            } finally {
                response2.close();
            }
            
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 

可以用一种更简单、更灵活的API:

    Content content = Request.Get("http://www.sina.com.cn").execute().returnContent();
    System.out.println(content.getType());
    Request.Post("http://targethost/login")
      .bodyForm(Form.form().add("username",  "vip").add("password",  "secret").build())
      .execute().returnContent();

英文原始地址:http://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html

转载请注明:子暃之路 » Apache HttpComponet(4)–HttpClient4.3快速开始

喜欢 (4)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址