use of org.apache.http.entity.mime.content.StringBody in project ninja by ninjaframework.
the class NinjaTestBrowser method uploadFileWithForm.
public String uploadFileWithForm(String url, String paramName, File fileToUpload, Map<String, String> formParameters) {
String response = null;
try {
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// For File parameters
entity.addPart(paramName, new FileBody((File) fileToUpload));
// add form parameters:
if (formParameters != null) {
for (Entry<String, String> parameter : formParameters.entrySet()) {
entity.addPart(parameter.getKey(), new StringBody(parameter.getValue()));
}
}
post.setEntity(entity);
// Here we go!
response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
post.releaseConnection();
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
use of org.apache.http.entity.mime.content.StringBody in project mzmine2 by mzmine.
the class LibrarySubmitTask method submitGNPS.
/**
* Submit json library entry to GNPS webserver
*
* @param json
*/
private void submitGNPS(String json) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
MultipartEntity entity = new MultipartEntity();
// ######################################################
// NEEDED
// user pass and json entry
//
entity.addPart("username", new StringBody(USER));
entity.addPart("password", new StringBody(PASS));
entity.addPart("spectrum", new StringBody(json));
// job description is not entry description
entity.addPart("description", new StringBody(SOURCE_DESCRIPTION));
HttpPost httppost = new HttpPost(GNPS_LIBRARY_SUBMIT_URL);
httppost.setEntity(entity);
log.info("Submitting GNPS library entry " + httppost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
writeResults("GNPS submit entry response status: " + response.getStatusLine(), Result.INFO);
log.info("GNPS submit entry response status: " + response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
log.info("GNPS submit entry response content length: " + resEntity.getContentLength());
writeResults("GNPS submit entry response content length: " + resEntity.getContentLength(), Result.SUCCED);
String body = IOUtils.toString(resEntity.getContent());
String url = "https://gnps.ucsd.edu/ProteoSAFe/status.jsp?task=" + body;
log.log(Level.INFO, "Submission task: " + url);
writeResults(url, Result.SUCCED, true);
EntityUtils.consume(resEntity);
} else {
log.warning("Not submitted to GNPS:\n" + json);
writeResults("Not submitted to GNPS\n" + json, Result.ERROR);
}
} finally {
response.close();
}
} finally {
httpclient.close();
}
} catch (IOException e) {
log.log(Level.SEVERE, "Error while submitting GNPS job", e);
throw new MSDKRuntimeException(e);
}
}
Aggregations