Skip to content
/ urlib Public

使用Java原生库封装一个简单好用的Http请求工具类

Notifications You must be signed in to change notification settings

flycati/urlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

示例

  • 发送get请求
 UrlResp res = Urllib.builder()
         .setUrl("http://httpbin.org")
         .addPathVariable("get")
         .addQuery("keyword", "csdn")
         .addHeader("User-Agent", "Chrome")
         .addHeader("Content-Type", "text/html")
         .addCookies("JSESSIONID=2454;aie=adf")
         .addCookie("username", "bin")
         .get();

 if (res.getResponseCode() == UrlResp.HTTP_OK){
     System.out.println(res.getText());
 }

打印结果

在这里插入图片描述

  • 发送post请求并携带表单数据
public void test(){
    UrlResp res = Urllib.builder()
            .setUrl("http://httpbin.org")
            .addPathVariable("post")
            .addQuery("keyword", "csdn")
            .addHeader("User-Agent", "Chrome")
            .addHeader("Content-Type", "application/x-www-form-urlencoded")
            .addCookies("JSESSIONID=2454;aie=adf")
            .addCookie("yes", "no")
            .addPostData("username", "zhangsan")
            .addPostData("password", "154134513")
            .addPostData("gender", "male")
            .addPostData("age", 18)
            .post();

    if (res.getResponseCode() == UrlResp.HTTP_OK){
        System.out.println(res.getText());
    }
}

打印结果

在这里插入图片描述

  • 发送post请求并携带json格式数据

构造json数据稍微复杂一点,毕竟不能像python和php语言。

public void test(){
   Map data = new HashMap();

    Map keyword1 = new HashMap();
    keyword1.put("value", "98gadf");
    data.put("keyword1", keyword1);

    Map keyword2 = new HashMap();
    keyword1.put("value", "9fghfsgdf");
    data.put("keyword2", keyword1);

    List list = new ArrayList();
    list.add("a");
    list.add("b");
    list.add("c");


    UrlResp res = Urllib.builder()
            .setUrl("http://httpbin.org")
            .addPathVariable("post")
            .addHeader("User-Agent", "Chrome")
            .addHeader("Content-Type", "application/json")
            .addPostData("touser", "orafd98bu")
            .addPostData("template_id", "34589u")
            .addPostData("page", 1)
            .addPostData("form_id", 345)
            .addPostData("data", data)
            .addPostData("list", list)
            .post();

    if (res.getResponseCode() == UrlResp.HTTP_OK){
        System.out.println(res.getText());
    }
}

打印结果

在这里插入图片描述

  • 保存二进制文件
@Test
public void test() throws IOException {
    UrlResp res = Urllib.builder()
            .setUrl("https://img-blog.csdnimg.cn/20190324160739729.png")
            .addHeader("User-Agent", "Chrome")
            .get();

    if (res.getResponseCode() == UrlResp.HTTP_OK){
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(
                        new File("photo.png")
                )
        );
        os.write(res.getByteContent());
    }
}

输出结果

在这里插入图片描述

About

使用Java原生库封装一个简单好用的Http请求工具类

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages