Code Monkey home page Code Monkey logo

urlib's Introduction

示例

  • 发送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());
    }
}

输出结果

在这里插入图片描述

urlib's People

Contributors

bryonzen avatar

Stargazers

 风一样的码农  avatar  avatar Jackie Moo avatar  avatar  avatar WEIWC avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.