OkHttp 简易封装工具 FastHttpClient

码农软件 · 软件分类 · 网络工具包 · 2019-02-25 07:13:19

软件介绍

FastHttpClient 是简易封装 OkHttp 的工具(需要jdk8以上),高效使用http(s) post get 。

用法

1.同步的get

FastHttpClient.get().
        url(url).
        addParams("userName", "icecool").
        addParams("password", "111111").
        build().
        execute();

2.同步的post

FastHttpClient.post().
        url(url).
        addParams("userName", "icecool").
        addParams("password", "111111").
        build().
        execute();

3.异步的get

FastHttpClient.get().
            url(url).
            addParams("userName","icecool").
            addParams("password", "111111").
            build().
            executeAsync(new Callback() {
            @Override
            public void onFailure(Call call, Exception e, int id) {
                //TODO
            }

            @Override
            public void onResponse(Call call,Response response, int id) {
                try {
                    System.out.println(response.body().string());
                } catch (IOException e) {
                }
            }
        });

4.异步的post

FastHttpClient.post().
            url(url).
            addParams("userName","icecool").
            addParams("password", "111111").
            build().
            executeAsync(new Callback() {
            @Override
            public void onFailure(Call call, Exception e, int id) {
                //TODO
            }

            @Override
            public void onResponse(Call call,Response response, int id) {
                try {
                    System.out.println(response.body().string());
                } catch (IOException e) {
                }
            }
        });

5.异步下载文件

FastHttpClient.get().
        url("http://e.hiphotos.baidu.com/image/pic/item/faedab64034f78f0b31a05a671310a55b3191c55.jpg").
        build().addNetworkInterceptor(new DownloadFileInterceptor(){
            @Override
            public void updateProgress(long downloadLenth, long totalLength, boolean isFinish) {
                System.out.println("updateProgress downloadLenth:"+downloadLenth+
                        ",totalLength:"+totalLength+",isFinish:"+isFinish);
            }
        }).
        executeAsync(new DownloadFileCallback("/tmp/tmp.jpg") {//save file to /tmp/tmp.jpg
                @Override
                public void onFailure(Call call, Exception e, int id) {
                    e.printStackTrace();
                }
                @Override
                public void onSuccess(Call call, File file, int id) {
                    super.onSuccess(call, file, id);
                    System.out.println("filePath:"+file.getAbsolutePath());
                }
                @Override
                public void onSuccess(Call call, InputStream fileStream, int id) {
                    System.out.println("onSuccessWithInputStream");
                }
        });
Thread.sleep(50000);

6.上传文件

byte[] imageContent=FileUtil.getBytes("/tmp/test.png");
        response = FastHttpClient.post().
                url(url).
                addFile("file1", "a.txt", "123").
                addFile("file2", "b.jpg", imageContent).
                build().
                connTimeOut(10000).
                execute();
System.out.println(response.body().string());

7.https get

Response response = FastHttpClient.get().url("https://kyfw.12306.cn/otn/").
                build()
                .execute();
System.out.println(response.body().string());

8.https post

Response response = FastHttpClient.post().url("https://kyfw.12306.cn/otn/").
                build()
                .execute();
System.out.println(response.body().string());

本文地址:https://www.codercto.com/soft/d/77.html

网站入侵与脚本技术快速防杀

网站入侵与脚本技术快速防杀

2011-1 / 56.00元

《反黑风暴·网站入侵与脚本技术快速防杀》由浅入深、图文并茂地再现了网站入侵与脚本技术快速防杀的全过程,内容涵盖:Windows系统编程基础、黑客程序的配置和数据包嗅探、Web脚本攻击与防御、基于Web的DDoS攻击与防御、流行的黑客编程技术、XSS跨站脚本攻击技术与防范、Cookie欺骗与防御技术剖析、数据库入侵与防范技术、SQL注入攻击与防范、网络上传漏洞的攻击与防范、系统后门编程技术、编程攻击......一起来看看 《网站入侵与脚本技术快速防杀》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具