-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMyHttpClient.java
More file actions
51 lines (44 loc) · 1.36 KB
/
MyHttpClient.java
File metadata and controls
51 lines (44 loc) · 1.36 KB
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
package HttpServerClient;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpMethod;
public class MyHttpClient {
private HttpClient client;
public MyHttpClient() {
this.client = new HttpClient();
try {
this.client.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ContentResponse sendRequest(String url, String method, HashMap<Object, Object> headers, String body) {
ContentResponse contentRes;
HttpMethod httpMethod = HttpMethod.fromString(method);
Request request = client.newRequest(url).method(httpMethod);
try {
//setting header
for(Entry<Object,Object> o:headers.entrySet()){
request.header(o.getKey().toString(), o.getValue().toString());
}
contentRes= request.send();
return contentRes;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}