-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerRequest.java
More file actions
103 lines (90 loc) · 2.59 KB
/
ServerRequest.java
File metadata and controls
103 lines (90 loc) · 2.59 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
SPlaT project: Object that requests a search from a web server,
Singleton design : written by Shafik Amin, 5-24-2003
*/
/** Imports **/
import java.net.*;
import java.io.*;
import java.util.*;
public class ServerRequest
{
private static final String[] serverScripts =
{
"http://www.google.com/search",
};
/* Static Instance Factory, returns null if not supported */
public static ServerRequest getServerRequestFrom(String filename)
{
try { return new ServerRequest(filename); }
catch (Exception e) { return null; }
}
/** Instance variables **/
private URL myURL;
private String filename;
private Properties props;
/* constructors, private for instance control */
private ServerRequest(String filename) throws Exception
{
props = new Properties();
FileInputStream in = new FileInputStream(filename);
props.load(in);
myURL = new URL(props.getProperty("URL"));
props.remove("URL");
this.filename = filename;
}
/*
Sends a search request to the server, takes a filename
that stores all the request's properties,
returns an input stream.
*/
public String getRequestResult()
{
try
{
/* Send request */
URLConnection connection = myURL.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(connection.getOutputStream());
Enumeration enu = props.keys();
while (enu.hasMoreElements())
{
String key = (String)enu.nextElement();
String value = props.getProperty(key);
out.print(key);
System.out.println(key);
out.print('=');
System.out.println('=');
out.print(URLEncoder.encode(value, "UTF-8"));
System.out.println(value);
if (enu.hasMoreElements())
out.print('&');
System.out.println("-------");
}
out.close();
/* Read response */
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String line;
StringBuffer ret = new StringBuffer();
while ((line = in.readLine()) != null)
ret.append(line + "\n");
in.close();
return ret.toString();
}
catch (Exception e)
{
System.out.println("REQUEST FAILED: " + "\n" + e);
}
return null;
}
/** Test **/
public static void main(String[] args)
{
ServerRequest s = ServerRequest.getServerRequestFrom("request.txt");
System.out.println(s.getRequestResult());
}
}