• 获取相应头信息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来解析。

  • 相关阅读:
    查看Java中每个数据类型所占的位数,和表示数值的范围,并得出结论。
    同名变量的屏蔽原则
    反码,补码,原码
    机器学习概念性知识总结
    图的最短路径问题
    System Design 笔记及代码(系统设计)
    2016网易游戏实习算法题解(今年找暑期实习的时候参加的)
    Google java style
    18.1---不用加号的加法(CC150)
    Java模板模式(template)
  • 原文地址:https://www.cnblogs.com/mengxinrenyu/p/7633537.html
Copyright © 2020-2023  润新知