HttpClient配置及運用(一)

  HttpComponents是Apache 旗下的項目。其中有一個HttpClient,即HTTP客戶端。javascript

以後咱們創建項目,導入jar包,開始開發css

  1、獲取網頁內容:html

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(); //一、建立實例
        //CloseableHttpClient意思是:可關閉的
        HttpGet httpGet=new HttpGet("http://www.huimsoft.com"); //二、建立實例
         
        //限制類型http://www.tuicool.com/
         
        CloseableHttpResponse closeableHttpResponse=null;
        try {
            closeableHttpResponse=closeableHttpClient.execute(httpGet); //三、執行
        } 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(); //四、獲取實體
 
        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在方法上拋出,以簡化代碼格式,讓其更好看。java

  

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(); //一、建立實例
        HttpGet httpGet=new HttpGet("http://www.huimsoft.com"); //二、建立請求
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //三、執行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //四、獲取實體
        System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //五、獲取網頁內容,而且指定編碼
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

  2、設置請求頭信息jquery

 設置請求頭信息的目的是爲了模擬瀏覽器apache

  

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(); //一、建立實例
        HttpGet httpGet=new HttpGet("http://www.tuicool.com"); //二、建立請求
         
        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); //三、執行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //四、獲取實體
        System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //五、獲取網頁內容,而且指定編碼
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
 
}

3、獲取相應頭信息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(); //一、建立實例
        HttpGet httpGet=new HttpGet("http://www.tuicool.com"); //二、建立請求
         
        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); //三、執行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //四、獲取實體
         
        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")); //五、獲取網頁內容,而且指定編碼
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

運行輸出:app

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

咱們的重點是獲取的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(); //一、建立實例
        HttpGet httpGet=new HttpGet("http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"); //二、建立實例
         
        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); //三、執行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //四、獲取實體
         
        Header header=httpEntity.getContentType();
        System.out.println(header.getValue().toString()); //這是獲取value
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

運行結果:

運行結果:application/javascript

若是請求一個圖片,同上

運行結果:image/png

相關文章
相關標籤/搜索