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();
                }
            }
            
        }
    }
  • 相关阅读:
    为调试JavaScript添加输出窗口
    后悔自己没有学好数学
    IEnumeralbe<T>被误用一例
    开发软件真是一件有意思的事情
    在网页上实现WinForm控件:ComboBox
    WinForm异步编程中一个容易忽视的问题
    网页上的DataGridView
    用Excel生成代码
    游戏处女作 打方块
    用GDI+保存Image到流时的一个有趣现象
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/6278222.html
Copyright © 2011-2022 走看看