-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWSTest.java
More file actions
63 lines (51 loc) · 1.83 KB
/
WSTest.java
File metadata and controls
63 lines (51 loc) · 1.83 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
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class WSTest {
private static final String endpoint = "http://localhost:5000/message";
public static void main(String[] args) {
try{
int i = 1;
while(true){
URL urlRequest = new URL(endpoint);
HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
String msg= "this is a message" + i;
String payLoad = "{\"namespace\":\"udit\", \"text\":\"" + msg + "\"}";
//System.out.println(payLoad);
OutputStream os = conn.getOutputStream();
os.write(payLoad.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode() + conn.getResponseMessage());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
Thread.sleep(1);
i++;
}
}
catch(MalformedURLException ex){
ex.printStackTrace();
}
catch(IOException ex){
ex.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}