zoukankan      html  css  js  c++  java
  • CloseableHttpClient(二)

    package com.cmy.httpClient;
    
    import java.io.IOException;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.junit.Test;
    
    
    public class HttpClientTest {
    
        @Test
        public void jUnitTest(){
            get();
        }
    
        public void get(){
            CloseableHttpClient client = HttpClients.createDefault();
            try {
                HttpGet httpget = new HttpGet("http://www.baidu.com/");
                HttpPost httppost = new HttpPost("http://www.mi.com/en/");
                System.out.println("executing resutlt url: "+httpget.getURI());
                System.out.println("executing resutlt url: "+httppost.getURI());
                System.out.println("executing resutlt url: "+httpget.getMethod());
                System.out.println("executing resutlt url: "+httppost.getMethod());
                
                CloseableHttpResponse response = client.execute(httppost);
                try {
                    HttpEntity entity = response.getEntity();
                    System.out.println("响应状态: "+response.getStatusLine());
                    if(entity!=null){
                        System.out.println("响应长度: "+entity.getContentLength());
                        System.out.println("响应内容: "+EntityUtils.toString(entity));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
        }
    }
  • 相关阅读:
    leecode 91. 解码方法
    leecode 166. 分数到小数
    剑指 Offer 31. 栈的压入、弹出序列
    leecode 386. 字典序排数
    LeetCode 311 稀疏矩阵的乘法
    leecode 89. 格雷编码
    leecode 79. 单词搜索
    leecode 207. 课程表
    QT -- 解决:Error: Could not decode "*.cpp" with "UTF-8"
    VS+QT -- 没有PRO文件的问题
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/6278222.html
Copyright © 2011-2022 走看看