-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomNetworking1.java
More file actions
35 lines (28 loc) · 1.17 KB
/
CustomNetworking1.java
File metadata and controls
35 lines (28 loc) · 1.17 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
import java.net.*;
import java.io.*;
class CustomNetworking1{
static final String ADDRESS = "http://localhost:9999/Test1/Servlet1";
static final String MESSAGE = "Hello world, from User";
public static void main(String[] args) throws Exception{
URL url=new URL(ADDRESS);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(MESSAGE.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
dos.writeBytes(MESSAGE);
dos.flush();
dos.close();
InputStream in=connection.getInputStream();
int c;
while((c=in.read()) != -1){
System.out.print((char)c);
}
}
}