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

  • 相关阅读:
    SQL SERVER中一些常见性能问题的总结
    【BZOJ2554】Color 概率神题
    【BZOJ1818】[Cqoi2010]内部白点 扫描线+树状数组
    【BZOJ1879】[Sdoi2009]Bill的挑战 状压DP
    【BZOJ2668】[cqoi2012]交换棋子 费用流
    【BZOJ4372】烁烁的游戏 动态树分治+线段树
    【BZOJ3470】Freda’s Walk 概率与期望
    【BZOJ2087】[Poi2010]Sheep 几何+DP
    【BZOJ4428】[Nwerc2015]Debugging调试 记忆化搜索+分块
    【BZOJ2137】submultiple 高斯消元求伯努利数
  • 原文地址:https://www.cnblogs.com/mengxinrenyu/p/7633537.html
Copyright © 2020-2023  润新知