Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created August 22, 2011 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mryoshio/1161766 to your computer and use it in GitHub Desktop.
Save mryoshio/1161766 to your computer and use it in GitHub Desktop.
build POST request which has multipart body (String and Image)
private HttpPost buildPostRequest(String _method, String _path, Map _params)
throws ServerConnectionException {
HttpPost request = new HttpPost();
try {
request.setURI(URIUtils.createURI(scheme, host, port, _path, null,
null));
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
for (Iterator it = _params.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
if (_params.get(key) instanceof File) {
FileBody fileBody = new FileBody((File) _params.get(key));
entity.addPart(key, fileBody);
} else {
entity.addPart(key,
new StringBody((String) _params.get(key)));
}
}
request.setEntity(entity);
} catch (Exception e) {
new ServerConnectionException(e);
}
return request;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment