-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.java
43 lines (33 loc) · 1.45 KB
/
Post.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
package post;
import java.net.URL;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Post {
public static void main(String[] args)throws IOException {
URL url = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "Application/json;utf-8");
connection.setDoOutput(true);
String Post_data = "{User-ID: 10, ID:101}";
OutputStream output = connection.getOutputStream();
output.write(Post_data.getBytes());
output.flush();
output.close();
int response = connection.getResponseCode();
System.out.println("Response " + response);
System.out.println("Response Message " + connection.getResponseMessage());
if (response == HttpURLConnection.HTTP_CREATED) {
BufferedReader read = new BufferedReader(new InputStreamReader(connection.getInputStream()));//read
StringBuffer str = new StringBuffer();
String line = null;
while ((line = read.readLine()) != null) {
str.append(line);
} read.close();
System.out.println("POST Response " + str.toString());
}
}
}