throw new RuntimeException("You cannot send both form parameters and an entity body");
if (!request.getFormParameters().isEmpty())
{
commitHeaders(request, httpMethod);
HttpPost post = (HttpPost) httpMethod;
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, List<String>> formParam : request.getFormParameters().entrySet())
{
List<String> values = formParam.getValue();
for (String value : values)
{
formparams.add(new BasicNameValuePair(formParam.getKey(), value));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(entity);
}
else if (request.getBody() != null)
{
if (httpMethod instanceof HttpGet) throw new RuntimeException("A GET request cannot have a body.");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
request.writeRequestBody(request.getHeadersAsObjects(), baos);
ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray())
{
@Override
public Header getContentType()
{
return new BasicHeader("Content-Type", request.getBodyContentType().toString());
}
};
HttpPost post = (HttpPost) httpMethod;
commitHeaders(request, httpMethod);
post.setEntity(entity);
}
catch (IOException e)
{
throw new RuntimeException(e);
}