HttpClient 工具类封装

http-client-utli

httpClient 工具类 github地址:https://github.com/yangwenjie88/http-client-utli/

现在微服务这么火,在日常开发中我们经常需要调用http接口,虽然java自身提供了net相关工具包,但是其灵活性和功能总是不如人意,
于是有了apache httpclient 类库 ,大部分时候我们都是需要用的时候,百度一下,复制黏贴就使用,也不管它什么性能优化使用连接池等等,能调通就行。
在项目中对于这个工具类库也许没有进行很好的封装。在哪里使用就写在哪里,很多地方用到,就在多个地方写。反正就是复制粘贴,很方便,但是这样就会导致项目中大量代码冗余和五花八门的调用方法,
增大了维护的成本。所以这里对httpcient的常用操作封装成一个工具类

使用方法

/**
 * @author Yang WenJie
 * @date 2018/2/10 下午9:26
 */
public class HttpClientDemo {

    @Test
    public void get() throws IOException{
        HttpRequestConfig config = HttpRequestConfig.create().url("http://localhost:8090/peoples/");
        HttpRequestResult result = HttpClientUtils.get(config);
        System.out.println(result.getResponseText());
    }

    @Test
    public void post() throws IOException {
        HttpRequestConfig config = HttpRequestConfig.create().url("http://localhost:8090/peoples/")
                                    .addParam("id",2)
                                    .addParam("name","yangwenjie2")
                                    .addParam("age",19);
        HttpRequestResult result = HttpClientUtils.post(config);
        System.out.println(result.getResponseText());

    }

    @Test
    public void postJson() throws IOException {
        HttpRequestConfig config = HttpRequestConfig.create().url("http://localhost:8090/peoples/body")
                .httpEntityType(HttpEntityType.ENTITY_STRING)
                .json("{\"name\",\"杨文杰\"}");
        HttpRequestResult result = HttpClientUtils.post(config);
        System.out.println(result.getResponseText());
    }

    @Test
    public void postByte() throws IOException {
        HttpRequestConfig config = HttpRequestConfig.create().url("http://localhost:8090/peoples/body")
                .httpEntityType(HttpEntityType.ENTITY_BYTES)
                .bytes("{\"name\",\"杨文杰\"}".getBytes("UTF-8"));
        HttpRequestResult result = HttpClientUtils.post(config);
        System.out.println(result.getResponseText());
    }

    @Test
    public void postInputStream() throws IOException {
        HttpRequestConfig config = HttpRequestConfig.create().url("http://localhost:8090/peoples/inputStream")
                .httpEntityType(HttpEntityType.ENTITY_STRING)
                .json("{\"name\",\"杨文杰\"}");
        HttpRequestResult result = HttpClientUtils.post(config);
        System.out.println(result);
    }

    @Test
    public void down() throws IOException {
        String imgUrl = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png"; //百度logo
        File file = new File("/Users/yangwenjie/test/baidu.png");
        HttpClientUtils.down(HttpRequestConfig.create().url(imgUrl).out(new FileOutputStream(file)));
        if (file.exists()) {
            System.out.println("图片下载成功了!存放在:" + file.getPath());
        }

    }


}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,261评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,347评论 19 139
  • 今天遇到一个奇怪的错误,Assertion failure in -[AFHTTPRequestSerialize...
    柚子姑娘666阅读 1,677评论 0 0
  • 好早前因某事买的书,今天闲着没事翻来看看。 人间失格,即失去作为人的资格。 《人间失格》是太宰治投水自杀前的最后一...
    晓明_aac2阅读 2,187评论 0 0
  • 前段时间,一个不熟的同学找我借钱还信用卡。借的时候跟我说,今天是最后的还款日,也约好了还钱的日期。其实当时我正在写...
    gh9527阅读 3,339评论 0 0