/
HttpClientHelper.java
64 lines (47 loc) · 2.05 KB
/
HttpClientHelper.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
59
60
61
62
63
64
package com.aliyun.eci.demo.http;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpClientHelper {
private final static String SERVER_IP = "127.0.0.1";
private final static String SERVER_PORT = "8761";
private final static int CONNECTION_TIMEOUT = 10000;
private final static int SOCKET_TIMEOUT = 10000;
private final static int REQUEST_PER_MINUTE = 5;
public static String sendPost(String urlParam) throws IOException {
PostMethod postMethod = new PostMethod(urlParam);
postMethod.addRequestHeader("Content-Type", "application/json");
initClient().executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
return result;
}
public static String sendGet(String urlParam) throws IOException {
GetMethod getMethod = new GetMethod(urlParam);
getMethod.addRequestHeader("Content-Type", "application/json");
initClient().executeMethod(getMethod);
String result = getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
return result;
}
private static HttpClient initClient() {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(SOCKET_TIMEOUT);
return httpClient;
}
public static void main(String[] args) throws InterruptedException {
String url = String.format("http://%s:%s/hello", SERVER_IP, SERVER_PORT);
while (true) {
new Thread(() -> {
try {
System.out.println(sendGet(url));
} catch (IOException e) {
e.printStackTrace();
}
}).start();
Thread.sleep(60000 / REQUEST_PER_MINUTE);
}
}
}