1. 程式人生 > >獲取相應頭信息Response Headers的內容類型Content-Type

獲取相應頭信息Response Headers的內容類型Content-Type

getname print css 鍵值 static esp har .com char

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

我們請求一個圖片,代碼如下:

public class Demo {
	public static void main(String[] args) throws Exception {
		CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、創建實例
		HttpGet httpGet=new HttpGet("http://static.bootcss.com/www/assets/img/null.png?1505127079951"); //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();
	}
}

運行結果:image/png

我們獲取內容類型是為了將來能夠過濾掉一些類型。選出我們需要的然後jsoup來解析。

獲取相應頭信息Response Headers的內容類型Content-Type