zoukankan      html  css  js  c++  java
  • java+httpclient—— 一个稍微复杂的get请求

    package jkcs;
    
    import java.io.IOException;
    
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.ClientProtocolException;
    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;
    import org.apache.http.util.EntityUtils;
    
    public class jiekoumoni 
    {
    
        public static void main(String[] args) throws ClientProtocolException, IOException 
        {
        
            
            CloseableHttpClient client = HttpClients.createDefault();    //创建一个http客户端
            
            
            
            HttpGet httpGet = new HttpGet("http://www.baidu.com"); // 通过httpget方式来实现我们的get请求
            
            
            
            
            CloseableHttpResponse Response = client.execute(httpGet);  // 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
            
            
            //-----------------------------------------------------------------------
    
            
            System.out.println(Response.getStatusLine());  //HTTP/1.1 200 OK
            
            System.out.println(Response.getProtocolVersion());  //HTTP/1.1
            
            System.out.println(Response.getStatusLine().getStatusCode());  //200
            
            
            //-----------------------------------------------------------------------
            
            
            
            HttpEntity httpEntity = Response.getEntity();         //获取某个固定的响应头
            
            System.out.println(httpEntity.getContentType()); //Content-Type: text/html
            
            
    
            
            
            //-----------------------------------------------------------------------
            
            
            System.out.println(Response.getFirstHeader("Content-Type"));//Content-Type: text/html
            System.out.println(Response.getFirstHeader("Date"));   //Date: Sun, 03 May 2020 06:58:16 GMT
            
            //-----------------------------------------------------------------------
            
            Header[] headers = Response.getAllHeaders();    //获取所有响应头
            for (Header header : headers) 
            {
               System.out.println("Key : " + header.getName()+",         ," +" Value : " + header.getValue());
            }
            
            
            
            //-----------------------------------------------------------------------
            
            
            Response.close();  // 关闭
            
            
            
    
        }
    
    }

    执行结果:

    HTTP/1.1 200 OK
    HTTP/1.1
    200


    Content-Type: text/html
    Content-Type: text/html
    Date: Sun, 03 May 2020 06:58:16 GMT


    Key : Content-Type, , Value : text/html
    Key : Server, , Value : bfe
    Key : Date, , Value : Sun, 03 May 2020 06:58:16 GMT

    ===========================================================

    package jkcs;
    
    import java.io.IOException;
    
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.ClientProtocolException;
    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 jiekoumoni 
    {
    
        public static void main(String[] args) throws ClientProtocolException, IOException, Exception 
        {
        
            
            CloseableHttpClient client = HttpClients.createDefault();    //创建一个http客户端
            
            HttpGet httpGet = new HttpGet("http://httpbin.org/get");// 通过httpPost方式来实现我们的get请求
            
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");//伪装成浏览器请求
            
            CloseableHttpResponse Response = client.execute(httpGet);  // 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
            
            
            //-----------------------------------------------------------------------
    
            
            System.out.println(Response.getStatusLine());  //HTTP/1.1 200 OK
            
            System.out.println(Response.getProtocolVersion());  //HTTP/1.1
            
            System.out.println(Response.getStatusLine().getStatusCode());  //200
            
            
            //-----------------------------------------------------------------------
            
            
            System.out.println(httpGet.getMethod());   //get
            
            System.out.println(httpGet.getURI());    //http://httpbin.org/get
            
            //-----------------------------------------------------------------------
            
            HttpEntity httpEntity = Response.getEntity();         //获取某个固定的响应头
            
            System.out.println(httpEntity.getContentType()); //Content-Type: text/html
            
            
    
            
            
            //-----------------------------------------------------------------------
            
            
            System.out.println(Response.getFirstHeader("Content-Type"));//Content-Type: text/html
            System.out.println(Response.getFirstHeader("Date"));   //Date: Sun, 03 May 2020 06:58:16 GMT
            
            //-----------------------------------------------------------------------
            
            Header[] headers = Response.getAllHeaders();    //获取所有响应头
            for (Header header : headers) 
            {
               System.out.println(header.getName()+":    "+ header.getValue());
            }
            
            
            
            //-----------------------------------------------------------------------
            
    
            
            Response.close();  // 关闭
            
            
            
    
        }
    
    }

    执行结果:

    HTTP/1.1 200 OK
    HTTP/1.1
    200


    GET
    http://httpbin.org/get


    Content-Type: application/json
    Content-Type: application/json
    Date: Sun, 03 May 2020 09:04:40 GMT


    Date: Sun, 03 May 2020 09:04:40 GMT
    Content-Type: application/json
    Content-Length: 375
    Connection: keep-alive
    Server: gunicorn/19.9.0
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true

  • 相关阅读:
    后台管理系统文件三部曲之——第三部曲实现文件的查看预览
    后台管理系统文件三部曲之——第二部曲实现文件的下载
    后台管理系统文件三部曲之——第一部曲实现文件的上传
    数组includes的使用之在字典里获取数据
    在Swift3里面实现点击不同按钮播放不同声音的一种实现方法
    如何在Swift3中获取Json包的内容(unwrap Json package)
    点击TableView任一行跳转详情页面会跳转两次的解决办法
    UITableView数据不显示(在console中已显示相应数据)
    关于“代理”(delegate)我的一点浅见
    出现 warning "Unknown class _??????????View in Interface Builder file."的原因
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12822015.html
Copyright © 2011-2022 走看看