Adds method to post http requests using httputil and specifying custom http headers.

fix-message-formatting
Damian Minkov 12 years ago
parent a8453f66f1
commit 0dff89bf2d

@ -380,6 +380,68 @@ public static HTTPResponseResult postForm(String address,
* @return the result or null if send was not possible or * @return the result or null if send was not possible or
* credentials ask if any was canceled. * credentials ask if any was canceled.
*/ */
public static HTTPResponseResult postForm(String address,
String usernamePropertyName,
String passwordPropertyName,
ArrayList<String> formParamNames,
ArrayList<String> formParamValues,
int usernameParamIx,
int passwordParamIx,
RedirectHandler redirectHandler)
throws Throwable
{
return postForm(
address,
usernamePropertyName, passwordPropertyName,
formParamNames, formParamValues,
usernameParamIx, passwordParamIx,
redirectHandler,
null, null);
}
/**
* Posting form to <tt>address</tt>. For submission we use POST method
* which is "application/x-www-form-urlencoded" encoded.
* @param address HTTP address.
* @param headerParamNames additional header name to include
* @param headerParamValues corresponding header value to include
* @return the result or null if send was not possible or
* credentials ask if any was canceled.
*/
public static HTTPResponseResult postForm(String address,
List<String> headerParamNames,
List<String> headerParamValues)
throws Throwable
{
return postForm(address, null, null, null, null, -1, -1, null,
headerParamNames, headerParamValues);
}
/**
* Posting form to <tt>address</tt>. For submission we use POST method
* which is "application/x-www-form-urlencoded" encoded.
* @param address HTTP address.
* @param usernamePropertyName the property to use to retrieve/store
* username value if protected site is hit, for username
* ConfigurationService service is used.
* @param passwordPropertyName the property to use to retrieve/store
* password value if protected site is hit, for password
* CredentialsStorageService service is used.
* @param formParamNames the parameter names to include in post.
* @param formParamValues the corresponding parameter values to use.
* @param usernameParamIx the index of the username parameter in the
* <tt>formParamNames</tt> and <tt>formParamValues</tt>
* if any, otherwise -1.
* @param passwordParamIx the index of the password parameter in the
* <tt>formParamNames</tt> and <tt>formParamValues</tt>
* if any, otherwise -1.
* @param redirectHandler handles redirection, should we redirect and
* the actual redirect.
* @param headerParamNames additional header name to include
* @param headerParamValues corresponding header value to include
* @return the result or null if send was not possible or
* credentials ask if any was canceled.
*/
public static HTTPResponseResult postForm(String address, public static HTTPResponseResult postForm(String address,
String usernamePropertyName, String usernamePropertyName,
String passwordPropertyName, String passwordPropertyName,
@ -387,7 +449,9 @@ public static HTTPResponseResult postForm(String address,
ArrayList<String> formParamValues, ArrayList<String> formParamValues,
int usernameParamIx, int usernameParamIx,
int passwordParamIx, int passwordParamIx,
RedirectHandler redirectHandler) RedirectHandler redirectHandler,
List<String> headerParamNames,
List<String> headerParamValues)
throws Throwable throws Throwable
{ {
DefaultHttpClient httpClient; DefaultHttpClient httpClient;
@ -416,7 +480,9 @@ public static HTTPResponseResult postForm(String address,
formParamValues, formParamValues,
usernameParamIx, usernameParamIx,
passwordParamIx, passwordParamIx,
redirectHandler); redirectHandler,
headerParamNames,
headerParamValues);
authEx = null; authEx = null;
} }
@ -464,6 +530,8 @@ public static HTTPResponseResult postForm(String address,
* @param passwordParamIx the index of the password parameter in the * @param passwordParamIx the index of the password parameter in the
* <tt>formParamNames</tt> and <tt>formParamValues</tt> * <tt>formParamNames</tt> and <tt>formParamValues</tt>
* if any, otherwise -1. * if any, otherwise -1.
* @param headerParamNames additional header name to include
* @param headerParamValues corresponding header value to include
* @return the result or null if send was not possible or * @return the result or null if send was not possible or
* credentials ask if any was canceled. * credentials ask if any was canceled.
*/ */
@ -475,7 +543,9 @@ private static HttpEntity postForm(
ArrayList<String> formParamValues, ArrayList<String> formParamValues,
int usernameParamIx, int usernameParamIx,
int passwordParamIx, int passwordParamIx,
RedirectHandler redirectHandler) RedirectHandler redirectHandler,
List<String> headerParamNames,
List<String> headerParamValues)
throws Throwable throws Throwable
{ {
// if we have username and password in the parameters, lets // if we have username and password in the parameters, lets
@ -554,6 +624,16 @@ else if(i == passwordParamIx && creds != null)
// insert post values encoded. // insert post values encoded.
postMethod.setEntity(entity); postMethod.setEntity(entity);
if(headerParamNames != null)
{
for(int i = 0; i < headerParamNames.size(); i++)
{
postMethod.addHeader(
headerParamNames.get(i),
headerParamValues.get(i));
}
}
// execute post // execute post
return executeMethod( return executeMethod(
httpClient, postMethod, redirectHandler, parameters); httpClient, postMethod, redirectHandler, parameters);

Loading…
Cancel
Save