Permalink
Browse files

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.
  • Loading branch information...
raygreenwell committed Aug 17, 2012
1 parent 9decf8b commit de7f31b0f40e85924c516cac4c4abe122244ee66
Showing with 23 additions and 1 deletion.
  1. +23 −1 src/main/java/com/samskivert/net/HttpPostUtil.java
@@ -13,6 +13,9 @@
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Collections;
import java.util.Map;
import com.samskivert.util.ServiceWaiter; import com.samskivert.util.ServiceWaiter;
/** /**
@@ -28,7 +31,23 @@
* @param submission the entire submission eg "foo=bar&baz=boo&futz=foo". * @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 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 throws IOException, ServiceWaiter.TimeoutException
{ {
final ServiceWaiter<String> waiter = new ServiceWaiter<String>( final ServiceWaiter<String> waiter = new ServiceWaiter<String>(
@@ -41,6 +60,9 @@ public static String httpPost (final URL url, final String submission, int timeo
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setUseCaches(false); conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 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()); DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(submission); out.writeBytes(submission);

0 comments on commit de7f31b

Please sign in to comment.