1 Star 0 Fork 0

tanghc / http-helper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

http-helper

一个http请求工具

  • maven依赖
<dependency>
    <groupId>net.oschina.durcframework</groupId>
    <artifactId>http-helper</artifactId>
    <version>1.0.0</version>
</dependency>
  • 测试用例
/**
 * 普通get请求
 * @throws IOException
 */
@Test
public void get() throws IOException {
    String s = HttpHelper.get("http://localhost:8089/http/get")
            // 添加请求参数
            .parameter("id", "1")
            .parameter("name", "jim")
            .execute()
            .asString();
    System.out.println(s);
}

/**
 * post提交表单
 * @throws IOException
 */
@Test
public void postForm() throws IOException {
    Map<String, Object> form = new HashMap<>();
    form.put("id", 1);
    form.put("name", "tom");
    String s = HttpHelper.postForm("http://localhost:8089/http/postForm", form)
            .execute()
            .asString();
    System.out.println(s);
}

/**
 * post提交表单,并上传文件。multipart
 * @throws IOException
 */
@Test
public void postFormFile() throws IOException {
    Map<String, Object> form = new HashMap<>();
    UploadFile file = new UploadFile("file", "a.txt", "hello world".getBytes());
    form.put("id", 1);
    form.put("name", "tom");
    String s = HttpHelper.postForm("http://localhost:8089/http/postFormFile", form, file)
            .execute()
            .asString();
    System.out.println(s);
}

/**
 * post提交json
 * @throws IOException
 */
@Test
public void postJson() throws IOException {
    Map<String, Object> form = new HashMap<>();
    form.put("id", 1);
    form.put("name", "tom");
    String json = JSON.toJSONString(form);
    String s = HttpHelper.postJson("http://localhost:8089/http/postJson", json)
            .execute()
            .asString();
    System.out.println(s);
}

/**
 * 设置header
 * @throws IOException
 */
@Test
public void setHeader() throws IOException {
    String s = HttpHelper.get("http://localhost:8089/http/header")
            // 设置单个header
            .header("token", "xxx")
            .execute()
            .asString();
    System.out.println(s);

    Map<String, String> headers = new HashMap<>();
    headers.put("token", "bbbb");
    String s2 = HttpHelper.get("http://localhost:8089/http/header")
            // 设置全部header
            .headers(headers)
            .execute()
            .asString();
    System.out.println(s2);
}

/**
 * 返回流
 * @throws IOException
 */
@Test
public void getStream() throws IOException {
    InputStream inputStream = HttpHelper.get("http://localhost:8089/http/stream")
            .execute()
            .asStream();
    String s = IOUtils.toString(inputStream);
    System.out.println(s);
}

/**
 * 获取http状态
 * @throws IOException
 */
@Test
public void getStatus() throws IOException {
    ResponseResult responseResult = HttpHelper.get("http://localhost:8089/http/get")
            .parameter("id", "1")
            .parameter("name", "jim")
            .execute();
    int status = responseResult.getStatus();
    if (status == HttpStatus.SC_OK) {
        System.out.println(responseResult.asString());
    }
}

/**
 * 获取响应头
 * @throws IOException
 */
@Test
public void getHeaders() throws IOException {
    Map<String, Object> form = new HashMap<>();
    form.put("id", 1);
    form.put("name", "tom");
    String json = JSON.toJSONString(form);
    ResponseResult responseResult = HttpHelper.postJson("http://localhost:8089/http/responseHeader", json)
            .execute();
    Map<String, String> headers = responseResult.getHeaders();
    System.out.println(headers.get("code"));
    System.out.println(headers.get("token"));
    System.out.println(responseResult.asString());
}

/**
 * 获取HttpResponse对象
 * @throws IOException
 */
@Test
public void getResponse() throws IOException {
    ResponseResult responseResult = HttpHelper.get("http://localhost:8089/http/get")
            .parameter("id", "1")
            .parameter("name", "jim")
            .execute();
    HttpResponse response = responseResult.getResponse();
    Locale locale = response.getLocale();
    System.out.println(locale);
}

/**
 * 自定义
 * @throws IOException
 */
@Test
public void custom() throws IOException {
    String s = HttpHelper.create()
            .url("http://localhost:8089/http/get")
            .method("get")
            .parameter("id", "1")
            .parameter("name", "jim")
            .header("token", "xx")
            .execute()
            .asString();
    System.out.println(s);

    String json = "{ \"name\": \"Jim\" }";
    String s2 = HttpHelper.create()
            .url("http://localhost:8089/http/postJson")
            .method("POST")
            .header("token", "xx")
            .entity(new StringEntity(json, ContentType.APPLICATION_JSON))
            .execute()
            .asString();
    System.out.println(s2);
}
MIT License Copyright (c) 2023 tanghc Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

http请求工具 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/durcframework/http-helper.git
git@gitee.com:durcframework/http-helper.git
durcframework
http-helper
http-helper
master

搜索帮助