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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取相应头信息Response Headers的内容类型Content-Type相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

在哪里可以查看Response Headers信息? 如何查看网页的响应头

爬虫中什么是requests

Fiddler实战之请求头(request headers)和响应头(response headers)

爬虫:基本原理

获取跨域请求的自定义的response headers

爬虫基本原理