Allow a map of request properties to be specified.

This adds an overload. A better way might be to configure some sort of builder
and finish with request(), but that's a larger refactor and if I was doing
that there'd be plenty to fix.
This commit is contained in:
Ray J. Greenwell
2012-08-16 19:06:00 -07:00
parent 9decf8bfbb
commit de7f31b0f4
@@ -13,6 +13,9 @@ 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;
/**
@@ -28,7 +31,23 @@ public class HttpPostUtil
* @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 (final URL url, final String submission, int timeout)
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>(
@@ -41,6 +60,9 @@ public class HttpPostUtil
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);