-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestCookies.java
69 lines (56 loc) · 1.5 KB
/
TestCookies.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
59
60
61
62
63
64
65
66
67
68
69
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
/*
* Author: Devdatta Kulkarni
*
*/
public class TestCookies {
public static void main(String []args) {
Socket client = null;
try {
if (args.length < 2) {
System.out.println("Usage:");
System.out.println("java TestCookies <host> <resource>");
System.exit(0);
}
String host = args[0];
String resource = args[1];
int portToUse = 9080;
client = new Socket(host, portToUse);
OutputStream out = client.getOutputStream();
PrintWriter pout = new PrintWriter(out);
pout.write("GET " + resource + " " + "HTTP/1.1\n");
pout.write("Host: " + host + "\n");
pout.write("Cookie: not-a-stranger-anymore=friend; $Path=/session-example/session1; $Domain=localhost\n");
pout.write("\n");
pout.flush();
/* Read the data back */
System.out.println("Reading data from server.");
InputStream in = client.getInputStream();
byte[] buffer = new byte[200];
int n = - 1;
FileOutputStream f = new FileOutputStream("serveroutput.txt");
while ( (n = in.read(buffer)) != -1)
{
if (n > 0)
{
System.out.write(buffer, 0, n);
f.write(buffer, 0, n);
}
}
f.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}