-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClientTest.java
58 lines (50 loc) · 2.02 KB
/
HttpClientTest.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 technology.touchmars.webhook;
import com.paypal.http.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.util.Iterator;
public class HttpClientTest {
public static final String LINE_SEP = System.getProperty("line.separator");
public void init() {
try {
// URL url = new URL("http://www.google.com");
// HttpURLConnection con = (HttpURLConnection) url.openConnection();
// con.setRequestMethod("GET");
// int code = con.getResponseCode();
// String result = con.getContent().toString();
// System.out.println(code);
// System.out.println(result);
Environment env = () -> "https://developer.paypal.com/";
HttpClient client = new HttpClient(env);
HttpRequest<String> req = new HttpRequest<String>("/docs", "GET", String.class);
HttpResponse<String> resp = client.execute(req);
Integer statusCode = resp.statusCode();
Headers headers = resp.headers();
String responseData = resp.result();
StringBuilder sb = new StringBuilder();
sb.append(statusCode).append(LINE_SEP);
if (headers!=null)
headers.forEach( key -> {
if (key!=null)
sb.append(key.trim()).append("=").append(headers.header(key).trim()).append(LINE_SEP);
} );
sb.append(LINE_SEP).append(responseData);
System.out.println(sb.toString());
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
HttpClientTest t = new HttpClientTest();
t.init();
}
}