webRequestSettings.getRequestParameters().toArray(pairs);
httpMethod.setQueryString(NameValuePair.toHttpClient(pairs));
}
}
else { // POST as well as PUT
final EntityEnclosingMethod method = (EntityEnclosingMethod) httpMethod;
method.getParams().setContentCharset(webRequestSettings.getCharset());
final String queryString = url.getQuery();
method.setQueryString(queryString);
if (webRequestSettings.getRequestBody() != null) {
final String body = webRequestSettings.getRequestBody();
final String charset = webRequestSettings.getCharset();
method.setRequestEntity(new StringRequestEntity(body, null, charset));
}
// Note that this has to be done in two loops otherwise it won't
// be able to support two elements with the same name.
if (webRequestSettings.getEncodingType() == FormEncodingType.URL_ENCODED
&& method instanceof PostMethod) {
final PostMethod postMethod = (PostMethod) httpMethod;
for (final NameValuePair pair : webRequestSettings.getRequestParameters()) {
postMethod.removeParameter(pair.getName(), pair.getValue());
}
for (final NameValuePair pair : webRequestSettings.getRequestParameters()) {
postMethod.addParameter(pair.getName(), pair.getValue());
}
}
else if (FormEncodingType.MULTIPART == webRequestSettings.getEncodingType()) {
final List<PartBase> partList = new ArrayList<PartBase>();
for (final NameValuePair pair : webRequestSettings.getRequestParameters()) {
final PartBase newPart;
if (pair instanceof KeyDataPair) {
final KeyDataPair pairWithFile = (KeyDataPair) pair;
final String charset = webRequestSettings.getCharset();
newPart = buildFilePart(pairWithFile, charset);
}
else {
newPart = new StringPart(pair.getName(), pair.getValue(), webRequestSettings.getCharset());
newPart.setContentType(null); // Firefox and IE seem not to send a content type
}
newPart.setTransferEncoding(null); // Firefox and IE don't send transfer encoding headers
partList.add(newPart);
}
Part[] parts = new Part[partList.size()];
parts = partList.toArray(parts);
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
}
else { // for instance a PUT request
final String body = webRequestSettings.getRequestBody();
if (body != null) {
final String contentType = webRequestSettings.getAdditionalHeaders().get("Content-type");
final String charset = webRequestSettings.getCharset();
method.setRequestEntity(new StringRequestEntity(body, contentType, charset));
}
}
}
httpMethod.setRequestHeader("User-Agent", webClient_.getBrowserVersion().getUserAgent());