3-19 HttpClient Fluent Api

HttpClient Fluent API

概述

  • 更簡單、更方便使用HttpClient api

使用

Get請求

Request.Get(url).execute().returnContent().asString();

Post請求

Request.Post(url).bodyString(paramStr, ContentType.DEFAULT_TEXT)
    .execute().returnContent().asString();

// ContentType = application/x-www-form-urlencoded
Request.Post(url).bodyForm(paramForm).execute().saveContent(new File("foo.txt"));

緩存、複用認證信息

Executor executor = Executor.newInstance().auth(new HttpHost("hostname", port), "username", "password");
executor.execute(Request.Post(url).bodyForm(form)).returnContent().asString();

ResponseHandler

Request.Post(url).bodyString(paramStr, ContentType.DEFAULT_TEXT)
    .execute().handleResponse(new ResponseHandler<>() {

    });
發佈了26 篇原創文章 · 獲贊 0 · 訪問量 6399
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章