Datasets:

ArXiv:
License:
File size: 3,638 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import httpcli.FormBody;
import httpcli.HttpRequest;
import httpcli.HttpCli;
import httpcli.HttpUrl;
import httpcli.MultipartBody;
import httpcli.ResponseBody;
import httpcli.adapter.RespBodyFile;
import java.io.File;

public class RestTest {
    
    HttpCli cli = HttpCli.get()
            .setDebug(true)
    ;
    
    String get() throws Exception {
        HttpRequest request = new HttpRequest("GET", 
                "https://api.github.com/users/defunkt");

        return cli.execute(request, String.class);
    }
    
    String post(int id, String name, boolean active) throws Exception {
        FormBody reqBody = new FormBody()
                .add("id", id)
                .add("name", name)
                .add("active", active);
        
        HttpRequest request = new HttpRequest(
            "POST", "http://127.0.0.1/test.php", reqBody);

        return cli.execute(request, String.class);
    }
    
    String delete(int id) throws Exception {
        FormBody body = new FormBody()
            .add("id", id);

        HttpRequest request = new HttpRequest(
            "DELETE", "http://127.0.0.1/test.php", body);

        return cli.execute(request, String.class);
    }
    
    File download() throws Exception {
        HttpRequest request = new HttpRequest(
            "GET", "https://github.com/jesusbmx/HttpCli/raw/master/dist/httpcli.jar")
            .setTimeoutMs(5000 * 2 * 2);
        
        RespBodyFile adapter = new RespBodyFile(
            "C:\\Users\\Jesus\\Downloads\\httpcli.jar");
        
        return cli.execute(request, adapter);
        //return cli.execute(request, File.class);
    }
    
    String upload() throws Exception { 
        MultipartBody body = new MultipartBody()
          .addParam("nombre", "Elizabéth Magaña")
          .addFile("img", new File("C:\\Users\\jesus\\Pictures\\420089-Kycb_1600x1200.jpg"));
    
        HttpRequest request = new HttpRequest(
          "POST", "http://127.0.0.1/test.php", body);

        return cli.execute(request, String.class);
    }
    
     String run() throws Exception {
        HttpUrl url = new HttpUrl("https://api.github.com/users/defunkt");
        
        HttpRequest request = new HttpRequest("GET", url)
                .addHeader("Accept", "application/json");

        ResponseBody body = null;
        try {
            body = cli.execute(request);
//            body.code [200];
//            body.headers [
//               Keep-Alive: timeout=5, max=98
//                Server: Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30
//                Connection: Keep-Alive
//                Content-Length: 2245
//                Date: Sun, 13 Mar 2022 15:01:53 GMT
//                Content-Type: text/html; charset=UTF-8
//                X-Powered-By: PHP/5.6.30
//            ];
//            body.in [InputStream];
            return body.string();
        } finally {
            if (body != null) body.close();
        }
    }
    
    public static void main(String[] args) {
        RestTest api = new RestTest();
     
        try {
            
            String get = api.get();
            System.out.println(get);
            
            String post = api.post(1, "My name", true);
            System.out.println(post);
            
            String delete = api.delete(1);
            System.out.println(delete);
            
            File down = api.download();
            System.out.println(down);
            
            String up = api.upload();
            System.out.println(up);
            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}