forked from samskivert/samskivert
/
HttpPostUtil.java
97 lines (83 loc) · 3.52 KB
/
HttpPostUtil.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
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
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2012 Michael Bayne, et al.
// http://github.com/samskivert/samskivert/blob/master/COPYING
package com.samskivert.net;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import com.samskivert.util.ServiceWaiter;
/**
* Contains utility methods for doing a form post.
*/
public class HttpPostUtil
{
/**
* Return the results of a form post. Note that the http request takes place on another
* thread, but this thread blocks until the results are returned or it times out.
*
* @param url from which to make the request.
* @param submission the entire submission eg "foo=bar&baz=boo&futz=foo".
* @param timeout time to wait for the response, in seconds, or -1 for forever.
*/
public static String httpPost (URL url, String submission, int timeout)
throws IOException, ServiceWaiter.TimeoutException
{
return httpPost(url, submission, timeout, Collections.<String, String>emptyMap());
}
/**
* Return the results of a form post. Note that the http request takes place on another
* thread, but this thread blocks until the results are returned or it times out.
*
* @param url from which to make the request.
* @param submission the entire submission eg "foo=bar&baz=boo&futz=foo".
* @param timeout time to wait for the response, in seconds, or -1 for forever.
* @param requestProps additional request properties.
*/
public static String httpPost (
final URL url, final String submission, int timeout, final Map<String, String> requestProps)
throws IOException, ServiceWaiter.TimeoutException
{
final ServiceWaiter<String> waiter = new ServiceWaiter<String>(
(timeout < 0) ? ServiceWaiter.NO_TIMEOUT : timeout);
Thread tt = new Thread() {
@Override public void run () {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
for (Map.Entry<String, String> entry : requestProps.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(submission);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuilder buf = new StringBuilder();
for (String s; null != (s = reader.readLine()); ) {
buf.append(s);
}
reader.close();
waiter.postSuccess(buf.toString()); // yay
} catch (IOException e) {
waiter.postFailure(e); // boo
}
}
};
tt.start();
if (waiter.waitForResponse()) {
return waiter.getArgument();
} else {
throw (IOException) waiter.getError();
}
}
}