/
UrlRequest.java
58 lines (49 loc) · 1.27 KB
/
UrlRequest.java
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
package com.example.api;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class UrlRequest {
public static class A {
public static String props = null;
static {
try {
props = new String(UrlRequest.class.getResourceAsStream("a.properties").readAllBytes());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(props);
}
}
private URL url;
private Map<String, String> params;
private URLConnection conn;
public UrlRequest(URL url, Map<String, String> params) {
url = url;
params = params;
}
/**
* Performs a network request and returns the data sent in the response.
*
* @return
*/
public String doRequest() {
try {
conn = url.openConnection();
} catch (IOException e) {
return e.getMessage();
}
for (Map.Entry<String, String> e : params.entrySet()) {
conn.addRequestProperty(e.getKey(), e.getValue());
}
try {
conn.connect();
byte[] bytes = conn.getInputStream().readAllBytes();
return new String(bytes, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
}