zoukankan      html  css  js  c++  java
  • 获取相应头信息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来解析。

  • 相关阅读:
    Firemonkey绑定对象列表
    Firemonkey 自定义Button的Style
    Delphi中使用GDI+进行绘图(2)
    Delphi中使用GDI+进行绘图(1)
    很好用的一个类:TJvAppXMLFileStorage
    我的Win32开发抉择,Delphi老将复出
    实现动态的XML文件读写操作
    一个简单的统计图像主颜色的算法(C#源代码)
    开始使用THREE.JS
    C++应用程序在Windows下的编译、链接(一)概述
  • 原文地址:https://www.cnblogs.com/mengxinrenyu/p/7633537.html
Copyright © 2011-2022 走看看