1 package tim.prune.function.gpsies;
\r
3 import java.net.URLConnection;
\r
5 import java.io.IOException;
\r
7 import java.io.InputStream;
\r
8 import java.util.Random;
\r
9 import java.io.OutputStream;
\r
10 import java.io.FileInputStream;
\r
13 * Taken from Client HTTP Request class
\r
14 * @author Vlad Patryshev
\r
16 public class FormPoster
\r
18 private URLConnection _connection;
\r
19 private OutputStream _os = null;
\r
20 private static final Random random = new Random();
\r
21 private static final String boundary = "---------------------------"
\r
22 + randomString() + randomString() + randomString();
\r
25 protected void connect() throws IOException {
\r
26 if (_os == null) _os = _connection.getOutputStream();
\r
29 protected void write(char c) throws IOException {
\r
34 protected void write(String s) throws IOException {
\r
36 _os.write(s.getBytes());
\r
39 protected void newline() throws IOException {
\r
44 protected void writeln(String s) throws IOException {
\r
50 protected static String randomString() {
\r
51 return Long.toString(random.nextLong(), 36);
\r
54 private void boundary() throws IOException {
\r
60 * Creates a new multipart POST HTTP request on a freshly opened URLConnection
\r
62 * @param inConnection an already open URL connection
\r
63 * @throws IOException
\r
65 public FormPoster(URLConnection inConnection) throws IOException
\r
67 _connection = inConnection;
\r
68 _connection.setDoOutput(true);
\r
69 _connection.setRequestProperty("Content-Type",
\r
70 "multipart/form-data; boundary=" + boundary);
\r
74 * Creates a new multipart POST HTTP request for a specified URL
\r
76 * @param url the URL to send request to
\r
77 * @throws IOException
\r
79 public FormPoster(URL url) throws IOException {
\r
80 this(url.openConnection());
\r
84 * Creates a new multipart POST HTTP request for a specified URL string
\r
86 * @param urlString the string representation of the URL to send request to
\r
87 * @throws IOException
\r
89 public FormPoster(String urlString) throws IOException {
\r
90 this(new URL(urlString));
\r
94 private void writeName(String name) throws IOException
\r
97 write("Content-Disposition: form-data; name=\"");
\r
103 * adds a string parameter to the request
\r
104 * @param name parameter name
\r
105 * @param value parameter value
\r
106 * @throws IOException
\r
108 public void setParameter(String name, String value) throws IOException
\r
112 newline(); newline();
\r
116 private static void pipe(InputStream in, OutputStream out) throws IOException
\r
118 byte[] buf = new byte[500000];
\r
121 synchronized (in) {
\r
122 while((nread = in.read(buf, 0, buf.length)) >= 0) {
\r
123 out.write(buf, 0, nread);
\r
132 * adds a file parameter to the request
\r
133 * @param name parameter name
\r
134 * @param filename the name of the file
\r
135 * @param is input stream to read the contents of the file from
\r
136 * @throws IOException
\r
138 public void setParameter(String name, String filename, InputStream is) throws IOException
\r
142 write("; filename=\"");
\r
146 write("Content-Type: ");
\r
147 String type = URLConnection.guessContentTypeFromName(filename);
\r
148 if (type == null) type = "application/octet-stream";
\r
156 * adds a file parameter to the request
\r
157 * @param name parameter name
\r
158 * @param file the file to upload
\r
159 * @throws IOException
\r
161 public void setParameter(String name, File file) throws IOException {
\r
162 setParameter(name, file.getPath(), new FileInputStream(file));
\r
166 * adds a parameter to the request; if the parameter is a File, the file is uploaded,
\r
167 * otherwise the string value of the parameter is passed in the request
\r
168 * @param name parameter name
\r
169 * @param object parameter value, a File or anything else that can be stringified
\r
170 * @throws IOException
\r
172 public void setParameter(String name, Object object) throws IOException
\r
174 if (object instanceof File) {
\r
175 setParameter(name, (File) object);
\r
177 setParameter(name, object.toString());
\r
182 * posts the requests to the server
\r
183 * @return input stream with the server response
\r
184 * @throws IOException
\r
186 public InputStream post() throws IOException {
\r
190 return _connection.getInputStream();
\r
194 * post the POST request to the server, with the specified parameter
\r
195 * @param name parameter name
\r
196 * @param value parameter value
\r
197 * @return input stream with the server response
\r
198 * @throws IOException
\r
199 * @see setParameter
\r
201 public InputStream post(String name, Object value) throws IOException {
\r
202 setParameter(name, value);
\r
207 * post the POST request to the server, with the specified parameters
\r
208 * @param name1 first parameter name
\r
209 * @param value1 first parameter value
\r
210 * @param name2 second parameter name
\r
211 * @param value2 second parameter value
\r
212 * @return input stream with the server response
\r
213 * @throws IOException
\r
214 * @see setParameter
\r
216 public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException {
\r
217 setParameter(name1, value1);
\r
218 return post(name2, value2);
\r
222 * post the POST request to the server, with the specified parameters
\r
223 * @param name1 first parameter name
\r
224 * @param value1 first parameter value
\r
225 * @param name2 second parameter name
\r
226 * @param value2 second parameter value
\r
227 * @param name3 third parameter name
\r
228 * @param value3 third parameter value
\r
229 * @return input stream with the server response
\r
230 * @throws IOException
\r
231 * @see setParameter
\r
233 public InputStream post(String name1, Object value1, String name2, Object value2,
\r
234 String name3, Object value3) throws IOException
\r
236 setParameter(name1, value1);
\r
237 return post(name2, value2, name3, value3);
\r
241 * post the POST request to the server, with the specified parameters
\r
242 * @param name1 first parameter name
\r
243 * @param value1 first parameter value
\r
244 * @param name2 second parameter name
\r
245 * @param value2 second parameter value
\r
246 * @param name3 third parameter name
\r
247 * @param value3 third parameter value
\r
248 * @param name4 fourth parameter name
\r
249 * @param value4 fourth parameter value
\r
250 * @return input stream with the server response
\r
251 * @throws IOException
\r
252 * @see setParameter
\r
254 public InputStream post(String name1, Object value1, String name2, Object value2,
\r
255 String name3, Object value3, String name4, Object value4) throws IOException
\r
257 setParameter(name1, value1);
\r
258 return post(name2, value2, name3, value3, name4, value4);
\r
262 * post the POST request specified URL, with the specified parameter
\r
263 * @param name parameter name
\r
264 * @param value parameter value
\r
265 * @return input stream with the server response
\r
266 * @throws IOException
\r
267 * @see setParameter
\r
269 public static InputStream post(URL url, String name1, Object value1) throws IOException {
\r
270 return new FormPoster(url).post(name1, value1);
\r
274 * post the POST request to specified URL, with the specified parameters
\r
275 * @param name1 first parameter name
\r
276 * @param value1 first parameter value
\r
277 * @param name2 second parameter name
\r
278 * @param value2 second parameter value
\r
279 * @return input stream with the server response
\r
280 * @throws IOException
\r
281 * @see setParameter
\r
283 public static InputStream post(URL url, String name1, Object value1,
\r
284 String name2, Object value2) throws IOException
\r
286 return new FormPoster(url).post(name1, value1, name2, value2);
\r
290 * post the POST request to specified URL, with the specified parameters
\r
291 * @param name1 first parameter name
\r
292 * @param value1 first parameter value
\r
293 * @param name2 second parameter name
\r
294 * @param value2 second parameter value
\r
295 * @param name3 third parameter name
\r
296 * @param value3 third parameter value
\r
297 * @return input stream with the server response
\r
298 * @throws IOException
\r
299 * @see setParameter
\r
301 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2,
\r
302 String name3, Object value3) throws IOException
\r
304 return new FormPoster(url).post(name1, value1, name2, value2, name3, value3);
\r
308 * post the POST request to specified URL, with the specified parameters
\r
309 * @param name1 first parameter name
\r
310 * @param value1 first parameter value
\r
311 * @param name2 second parameter name
\r
312 * @param value2 second parameter value
\r
313 * @param name3 third parameter name
\r
314 * @param value3 third parameter value
\r
315 * @param name4 fourth parameter name
\r
316 * @param value4 fourth parameter value
\r
317 * @return input stream with the server response
\r
318 * @throws IOException
\r
319 * @see setParameter
\r
321 public static InputStream post(URL url, String name1, Object value1, String name2, Object value2,
\r
322 String name3, Object value3, String name4, Object value4) throws IOException
\r
324 return new FormPoster(url).post(name1, value1, name2, value2, name3, value3, name4, value4);
\r