1 package com.myjavatools.web;
3 import java.net.URLConnection;
5 import java.io.IOException;
6 import java.util.HashMap;
9 import java.io.InputStream;
10 import java.util.Random;
11 import java.io.OutputStream;
12 import java.io.FileInputStream;
13 import java.util.Iterator;
16 * <p>Title: Client HTTP Request class</p>
17 * <p>Description: this class helps to send POST HTTP requests with various form data,
18 * including files. Cookies can be added to be included in the request.</p>
19 * <p>Modified by Ted Pearson(ted@tedpearson.com) 5/4/09</p>
21 * @author Vlad Patryshev
24 public class ClientHttpRequest {
25 URLConnection connection;
26 OutputStream os = null;
27 Map cookies = new HashMap();
29 protected void connect() throws IOException {
30 if (os == null) os = connection.getOutputStream();
33 protected void write(char c) throws IOException {
38 protected void write(String s) throws IOException {
40 os.write(s.getBytes());
43 protected void newline() throws IOException {
48 protected void writeln(String s) throws IOException {
54 private static Random random = new Random();
56 protected static String randomString() {
57 return Long.toString(random.nextLong(), 36);
60 String boundary = "---------------------------" + randomString() + randomString() + randomString();
62 private void boundary() throws IOException {
68 * Creates a new multipart POST HTTP request on a freshly opened URLConnection
70 * @param connection an already open URL connection
73 public ClientHttpRequest(URLConnection connection) throws IOException {
74 this.connection = connection;
75 connection.setDoOutput(true);
76 connection.setRequestProperty("Content-Type",
77 "multipart/form-data; boundary=" + boundary);
81 * Creates a new multipart POST HTTP request for a specified URL
83 * @param url the URL to send request to
86 public ClientHttpRequest(URL url) throws IOException {
87 this(url.openConnection());
91 * Creates a new multipart POST HTTP request for a specified URL string
93 * @param urlString the string representation of the URL to send request to
96 public ClientHttpRequest(String urlString) throws IOException {
97 this(new URL(urlString));
101 private void postCookies() {
102 StringBuffer cookieList = new StringBuffer();
104 for (Iterator i = cookies.entrySet().iterator(); i.hasNext();) {
105 Map.Entry entry = (Map.Entry)(i.next());
106 cookieList.append(entry.getKey().toString() + "=" + entry.getValue());
109 cookieList.append("; ");
112 if (cookieList.length() > 0) {
113 connection.setRequestProperty("Cookie", cookieList.toString());
118 * adds a cookie to the requst
119 * @param name cookie name
120 * @param value cookie value
121 * @throws IOException
123 public void setCookie(String name, String value) throws IOException {
124 cookies.put(name, value);
128 * adds cookies to the request
129 * @param cookies the cookie "name-to-value" map
130 * @throws IOException
132 public void setCookies(Map cookies) throws IOException {
133 if (cookies == null) return;
134 this.cookies.putAll(cookies);
138 * adds cookies to the request
139 * @param cookies array of cookie names and values (cookies[2*i] is a name, cookies[2*i + 1] is a value)
140 * @throws IOException
142 public void setCookies(String[] cookies) throws IOException {
143 if (cookies == null) return;
144 for (int i = 0; i < cookies.length - 1; i+=2) {
145 setCookie(cookies[i], cookies[i+1]);
149 private void writeName(String name) throws IOException {
151 write("Content-Disposition: form-data; name=\"");
157 * adds a string parameter to the request
158 * @param name parameter name
159 * @param value parameter value
160 * @throws IOException
162 public void setParameter(String name, String value) throws IOException {
165 newline(); newline();
169 private static void pipe(InputStream in, OutputStream out) throws IOException {
170 byte[] buf = new byte[500000];
175 while((nread = in.read(buf, 0, buf.length)) >= 0) {
176 out.write(buf, 0, nread);
185 * adds a file parameter to the request
186 * @param name parameter name
187 * @param filename the name of the file
188 * @param is input stream to read the contents of the file from
189 * @throws IOException
191 public void setParameter(String name, String filename, InputStream is) throws IOException {
192 String type = connection.guessContentTypeFromName(filename);
193 if (type == null) type = "application/octet-stream";
194 setParameter(name, filename, is, type);
198 * adds a file parameter to the request
199 * @param name parameter name
200 * @param filename the name of the file
201 * @param is input stream to read the contents of the file from
202 * @param type Content-Type of the file
203 * @throws IOException
205 public void setParameter(String name, String filename, InputStream is, String type) throws IOException {
208 write("; filename=\"");
212 write("Content-Type: ");
220 * adds a file parameter to the request
221 * @param name parameter name
222 * @param file the file to upload
223 * @throws IOException
225 public void setParameter(String name, File file) throws IOException {
226 setParameter(name, file.getPath(), new FileInputStream(file));
230 * adds a parameter to the request; if the parameter is a File, the file is uploaded, otherwise the string value of the parameter is passed in the request
231 * @param name parameter name
232 * @param object parameter value, a File or anything else that can be stringified
233 * @throws IOException
235 public void setParameter(String name, Object object) throws IOException {
236 if (object instanceof File) {
237 setParameter(name, (File) object);
239 setParameter(name, object.toString());
244 * adds parameters to the request
245 * @param parameters "name-to-value" map of parameters; if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request
246 * @throws IOException
248 public void setParameters(Map parameters) throws IOException {
249 if (parameters == null) return;
250 for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
251 Map.Entry entry = (Map.Entry)i.next();
252 setParameter(entry.getKey().toString(), entry.getValue());
257 * adds parameters to the request
258 * @param parameters array of parameter names and values (parameters[2*i] is a name, parameters[2*i + 1] is a value); if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request
259 * @throws IOException
261 public void setParameters(Object[] parameters) throws IOException {
262 if (parameters == null) return;
263 for (int i = 0; i < parameters.length - 1; i+=2) {
264 setParameter(parameters[i].toString(), parameters[i+1]);
269 * posts the requests to the server, with all the cookies and parameters that were added
270 * @return input stream with the server response
271 * @throws IOException
273 public InputStream post() throws IOException {
279 tis = connection.getInputStream();
280 } catch (IOException e) {
281 tis = ((java.net.HttpURLConnection) connection).getErrorStream();
287 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
288 * @param parameters request parameters
289 * @return input stream with the server response
290 * @throws IOException
293 public InputStream post(Map parameters) throws IOException {
294 setParameters(parameters);
299 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
300 * @param parameters request parameters
301 * @return input stream with the server response
302 * @throws IOException
305 public InputStream post(Object[] parameters) throws IOException {
306 setParameters(parameters);
311 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
312 * @param cookies request cookies
313 * @param parameters request parameters
314 * @return input stream with the server response
315 * @throws IOException
319 public InputStream post(Map cookies, Map parameters) throws IOException {
321 setParameters(parameters);
326 * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
327 * @param cookies request cookies
328 * @param parameters request parameters
329 * @return input stream with the server response
330 * @throws IOException
334 public InputStream post(String[] cookies, Object[] parameters) throws IOException {
336 setParameters(parameters);
341 * post the POST request to the server, with the specified parameter
342 * @param name parameter name
343 * @param value parameter value
344 * @return input stream with the server response
345 * @throws IOException
348 public InputStream post(String name, Object value) throws IOException {
349 setParameter(name, value);
354 * post the POST request to the server, with the specified parameters
355 * @param name1 first parameter name
356 * @param value1 first parameter value
357 * @param name2 second parameter name
358 * @param value2 second parameter value
359 * @return input stream with the server response
360 * @throws IOException
363 public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException {
364 setParameter(name1, value1);
365 return post(name2, value2);
369 * post the POST request to the server, with the specified parameters
370 * @param name1 first parameter name
371 * @param value1 first parameter value
372 * @param name2 second parameter name
373 * @param value2 second parameter value
374 * @param name3 third parameter name
375 * @param value3 third parameter value
376 * @return input stream with the server response
377 * @throws IOException
380 public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException {
381 setParameter(name1, value1);
382 return post(name2, value2, name3, value3);
386 * post the POST request to the server, with the specified parameters
387 * @param name1 first parameter name
388 * @param value1 first parameter value
389 * @param name2 second parameter name
390 * @param value2 second parameter value
391 * @param name3 third parameter name
392 * @param value3 third parameter value
393 * @param name4 fourth parameter name
394 * @param value4 fourth parameter value
395 * @return input stream with the server response
396 * @throws IOException
399 public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException {
400 setParameter(name1, value1);
401 return post(name2, value2, name3, value3, name4, value4);
405 * posts a new request to specified URL, with parameters that are passed in the argument
406 * @param parameters request parameters
407 * @return input stream with the server response
408 * @throws IOException
411 public static InputStream post(URL url, Map parameters) throws IOException {
412 return new ClientHttpRequest(url).post(parameters);
416 * posts a new request to specified URL, with parameters that are passed in the argument
417 * @param parameters request parameters
418 * @return input stream with the server response
419 * @throws IOException
422 public static InputStream post(URL url, Object[] parameters) throws IOException {
423 return new ClientHttpRequest(url).post(parameters);
427 * posts a new request to specified URL, with cookies and parameters that are passed in the argument
428 * @param cookies request cookies
429 * @param parameters request parameters
430 * @return input stream with the server response
431 * @throws IOException
435 public static InputStream post(URL url, Map cookies, Map parameters) throws IOException {
436 return new ClientHttpRequest(url).post(cookies, parameters);
440 * posts a new request to specified URL, with cookies and parameters that are passed in the argument
441 * @param cookies request cookies
442 * @param parameters request parameters
443 * @return input stream with the server response
444 * @throws IOException
448 public static InputStream post(URL url, String[] cookies, Object[] parameters) throws IOException {
449 return new ClientHttpRequest(url).post(cookies, parameters);
453 * post the POST request specified URL, with the specified parameter
454 * @param name parameter name
455 * @param value parameter value
456 * @return input stream with the server response
457 * @throws IOException
460 public static InputStream post(URL url, String name1, Object value1) throws IOException {
461 return new ClientHttpRequest(url).post(name1, value1);
465 * post the POST request to specified URL, with the specified parameters
466 * @param name1 first parameter name
467 * @param value1 first parameter value
468 * @param name2 second parameter name
469 * @param value2 second parameter value
470 * @return input stream with the server response
471 * @throws IOException
474 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2) throws IOException {
475 return new ClientHttpRequest(url).post(name1, value1, name2, value2);
479 * post the POST request to specified URL, with the specified parameters
480 * @param name1 first parameter name
481 * @param value1 first parameter value
482 * @param name2 second parameter name
483 * @param value2 second parameter value
484 * @param name3 third parameter name
485 * @param value3 third parameter value
486 * @return input stream with the server response
487 * @throws IOException
490 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException {
491 return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3);
495 * post the POST request to specified URL, with the specified parameters
496 * @param name1 first parameter name
497 * @param value1 first parameter value
498 * @param name2 second parameter name
499 * @param value2 second parameter value
500 * @param name3 third parameter name
501 * @param value3 third parameter value
502 * @param name4 fourth parameter name
503 * @param value4 fourth parameter value
504 * @return input stream with the server response
505 * @throws IOException
508 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException {
509 return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3, name4, value4);