FastHttpClient

簡單易用的httpclient

封裝OkHttp3,對外提供了POST請求、GET請求、上傳文件、下載文件、https請求、cookie管理等功能

https://github.com/icecooly/FastHttpClient

功能點

  • 支持多線程異步請求
  • 支持Http/Https協議
  • 支持同步/異步請求
  • 支持異步延遲執行
  • 支持Cookie持久化
  • 支持JSON、表單提交
  • 支持文件和圖片上傳/批量上傳,支持同步/異步上傳,支持進度提示

簡單的例子

1.同步Get請求(訪問百度首頁,自動處理https單向認證)

String url="https://www.baidu.com";
String resp=FastHttpClient.get().url(url).build().execute().string();

2.異步Get請求(訪問百度首頁)

FastHttpClient.get().url("https://www.baidu.com").build().
    executeAsync(new StringCallback() {
        @Override
        public void onFailure(Call call, Exception e, int id) {
            logger.error(e.getMessage(),e);
        }
        @Override
        public void onSuccess(Call call, String response, int id) {
            logger.info("response:{}",response);
        }
    });

3.百度搜索關鍵字'微信機器人'

FastHttpClient.get().
            url("http://www.baidu.com/s").
            addParams("wd", "微信機器人").
            addParams("tn", "baidu").
            build().
            execute().
            string();

4.異步下載一張百度圖片,有下載進度,保存為/tmp/tmp.jpg

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");
                }
        });

5.上傳文件

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

6.設置網絡代理

Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1088));
Response response = FastHttpClient.
        newBuilder().
        addNetworkInterceptor(logging).
        proxy(proxy).
        build().
        get().
        url("http://www.baidu.com").
        build().
        execute();
logger.info(response.string());

7.設置Http頭部信息

String url="https://www.baidu.com";
Response response=FastHttpClient.
            get().
            addHeader("Referer","http://news.baidu.com/").
            addHeader("cookie", "uin=test;skey=111111;").
            url(url).
            build().
            execute();
System.out.println(response.string());
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,540評論 25 708
  • iOS網絡編程讀書筆記 Facade Tester客戶端門面模式的實例(被動版本化) 被動版本化,所以硬編碼URL...
    melouverrr閱讀 1,631評論 3 7
  • 從三月份找實習到現在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發崗...
    時芥藍閱讀 42,373評論 11 349
  • Arduino Uno并不自帶藍牙模塊,需要額外購買藍牙模塊。一種常見的藍牙模塊是HC-05,出場設置中默認名稱為...
    0e234f032049閱讀 15,882評論 1 5