-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
58 lines (45 loc) · 1.6 KB
/
Main.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
final String uri = "http://localhost/demo/sample/json";
String resquestBody = "{\"id\":1, \"foo\":\"baa\"}";
System.out.println("URL: " + uri);
System.out.println("RequestBody: " + resquestBody);
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStreamWriter output = new OutputStreamWriter(conn.getOutputStream());
output.write(resquestBody.toString());
output.flush();
Object[] param = new Object[2];
param[0] = conn.getResponseCode();
param[1] = conn.getResponseMessage();
System.out.printf("Status: %s - %s\n", param);
InputStreamReader input = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(input);
String line = null;
StringBuilder responseBody = new StringBuilder();
responseBody.append("responseBody:\n");
while ((line = br.readLine()) != null) {
responseBody.append("\t" + line + "\n");
}
conn.disconnect();
System.out.println(responseBody);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}