HttpClient配置及运用(一)

2021-05-19 13:30

阅读:345

标签:有一个   导入   https   try   null   char   请求   com   dem   

  HttpComponents是Apache 旗下的项目。其中有一个HttpClient,即HTTP客户端。

技术分享

之后我们建立项目,导入jar包,开始开发

技术分享

  一、获取网页内容:

package com.zhi.httpClient;
 
import java.io.IOException;
 
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class Demo {
    public static void main(String[] args) {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        //CloseableHttpClient意思是:可关闭的
        HttpGet httpGet=new HttpGet("http://www.huimsoft.com"); //2、创建实例
         
        //限制类型http://www.tuicool.com/
         
        CloseableHttpResponse closeableHttpResponse=null;
        try {
            closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        } catch (ClientProtocolException e) { //客户端协议异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) { //IO异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
 
        String str="";
        try {
            str=EntityUtils.toString(httpEntity, "utf-8"); //获取网页内容,并且指定编码
        } catch (ParseException e) { //解析异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(str);
         
        //关闭closeableHttpResponse
        try {
            closeableHttpResponse.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        //关闭closeableHttpClient
        try {
            closeableHttpClient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

  代码有很多,但是很多是在处理异常,所以我们也可以使用throws在方法上抛出,以简化代码格式,让其更好看。

  

package com.zhi.httpClient;
 
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class Test {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://www.huimsoft.com"); //2、创建请求
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
        System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //5、获取网页内容,并且指定编码
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

  二、设置请求头信息

 设置请求头信息的目的是为了模拟浏览器

  

package com.zhi.httpClient2;
 
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class Demo {
 
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://www.tuicool.com"); //2、创建请求
         
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
        System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //5、获取网页内容,并且指定编码
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
 
}

三、获取相应头信息Response Headers的内容类型Content-type

package com.zhi.httpClient2;
 
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
 
public class Demo {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://www.tuicool.com"); //2、创建请求
         
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
         
        Header header=httpEntity.getContentType();
        System.out.println(header.toString()); //获取头信息内容
        System.out.println(header.getName().toString()); //这是获取key
        System.out.println(header.getValue().toString()); //这是获取value
         
        //System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //5、获取网页内容,并且指定编码
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

运行输出:

Content-Type: text/html; charset=utf-8
Content-Type
text/html; charset=utf-8

我们的重点是获取的value的值,有些是text/html的,有些是其他的,比如我们请求一个js文件。

https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js

 

HttpGet httpGet=new HttpGet("http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js");
public class Demo {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"); //2、创建实例
         
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
         
        Header header=httpEntity.getContentType();
        System.out.println(header.getValue().toString()); //这是获取value
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

运行结果:

运行结果:application/javascript

如果请求一个图片,同上

运行结果:image/png

HttpClient配置及运用(一)

标签:有一个   导入   https   try   null   char   请求   com   dem   

原文地址:http://www.cnblogs.com/FrankLiner/p/7718699.html


评论


亲,登录后才可以留言!