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();
                }
            }
            
        }
    }
  • 相关阅读:
    ReSharper Tips—GotoImplementation
    Possible multiple enumeration of IEnumerable
    Hello, Razor!
    自话自说——POI使用需要注意一个地方
    css中怎么设置透明度的问题
    记录排查国标直播流播放卡顿的问题
    互联网上做广告的优点
    C#、.Net经典面试题集锦(一)
    什么是MFC
    C/S与B/S 的区别
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/6278222.html
Copyright © 2011-2022 走看看