List of usage examples for org.apache.http.entity.mime.content StringBody StringBody
public StringBody(final String text) throws UnsupportedEncodingException
From source file:org.deviceconnect.android.manager.test.MultipartTest.java
/** * PUT????????.//w w w.ja v a2 s. c o m */ public void testParsingMutilpartAsRequestParametersMethodPut() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME); builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION); try { MultipartEntity entity = new MultipartEntity(); entity.addPart(DConnectProfileConstants.PARAM_DEVICE_ID, new StringBody(getDeviceId())); entity.addPart(DConnectProfileConstants.PARAM_SESSION_KEY, new StringBody(getClientId())); entity.addPart(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, new StringBody(getAccessToken())); HttpPut request = new HttpPut(builder.toString()); request.setEntity(entity); JSONObject response = sendRequest(request); assertResultOK(response); } catch (UnsupportedEncodingException e) { fail(e.getMessage()); } catch (JSONException e) { fail(e.getMessage()); } }
From source file:eu.prestoprime.p4gui.connection.WorkflowConnection.java
public static String executeWorkflow(P4Service service, P4Workflow workflow, Map<String, String> dParamsString, Map<String, File> dParamsFile) throws WorkflowRequestException { if (dParamsString == null) dParamsString = new HashMap<>(); if (dParamsFile == null) dParamsFile = new HashMap<>(); dParamsString.put("wfID", workflow.toString()); try {//from ww w . ja v a 2 s.co m MultipartEntity part = new MultipartEntity(); for (String key : dParamsString.keySet()) { String value = dParamsString.get(key); part.addPart(key, new StringBody(value)); } for (String key : dParamsFile.keySet()) { File value = dParamsFile.get(key); part.addPart(key, new FileBody(value)); } String path = service.getURL() + "/wf/execute/" + workflow; logger.debug("Calling " + path); P4HttpClient client = new P4HttpClient(service.getUserID()); HttpRequestBase request = new HttpPost(path); request.setHeader("content-data", "multipart/form-data"); ((HttpPost) request).setEntity(part); HttpResponse response = client.executeRequest(request); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); String line; if ((line = reader.readLine()) != null) { logger.debug("Requested new job: " + line); return line; } } } else { logger.debug(response.getStatusLine().toString()); throw new WorkflowRequestException("Unable to request execution of workflow " + workflow + "..."); } } catch (IOException e) { e.printStackTrace(); } throw new WorkflowRequestException( "Something wrong in WorkflowConnection requesting a workflow: no wfID returned..."); }
From source file:it.unipi.di.acube.batframework.systemPlugins.ERDSystem.java
@Override public HashSet<Tag> solveC2W(String text) throws AnnotationException { lastTime = Calendar.getInstance().getTimeInMillis(); HashSet<Tag> res = new HashSet<Tag>(); try {//from ww w . ja v a 2s .com URL erdApi = new URL(url); HttpURLConnection connection = (HttpURLConnection) erdApi.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT); multipartEntity.addPart("runID", new StringBody(this.run)); multipartEntity.addPart("TextID", new StringBody("" + text.hashCode())); multipartEntity.addPart("Text", new StringBody(text)); connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue()); OutputStream out = connection.getOutputStream(); try { multipartEntity.writeTo(out); } finally { out.close(); } int status = ((HttpURLConnection) connection).getResponseCode(); if (status != 200) { BufferedReader br = new BufferedReader(new InputStreamReader(connection.getErrorStream())); String line = null; while ((line = br.readLine()) != null) System.err.println(line); throw new RuntimeException(); } BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = null; while ((line = br.readLine()) != null) { String mid = line.split("\t")[2]; String title = freebApi.midToTitle(mid); int wid; if (title == null || (wid = wikiApi.getIdByTitle(title)) == -1) System.err.println("Discarding mid=" + mid); else res.add(new Tag(wid)); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } lastTime = Calendar.getInstance().getTimeInMillis() - lastTime; return res; }
From source file:edu.scripps.fl.pubchem.web.session.WebSessionBase.java
protected void addFormParts(Node formNode, MultipartEntity entity, Set<String> ignore) throws UnsupportedEncodingException { FormControlVisitorSupport fcvs = new FormControlVisitorSupport(); formNode.accept(fcvs);/*from w w w . j a v a 2s . co m*/ for (Map.Entry<String, String> entry : fcvs.getFormParameters().entrySet()) { if (!ignore.contains(entry.getKey())) entity.addPart(entry.getKey(), new StringBody(entry.getValue())); } }
From source file:net.asplode.tumblr.Post.java
/** * @param blog/*from ww w . j a v a2 s. co m*/ * The blog to post to. * @throws UnsupportedEncodingException */ public void setBlog(String blog) throws UnsupportedEncodingException { entity.addPart("group", new StringBody(blog)); }
From source file:uk.co.jarofgreen.cityoutdoors.API.BaseCall.java
protected void addDataToCall(String key, Integer value) { if (value != null) { try {//from w w w .j ava2 s . com multipartEntity.addPart(key, new StringBody(Integer.toString(value))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
From source file:niclients.main.pubni.java
/** * Creates NI publish HTTP POST signal./*from w ww. j av a2 s .c o m*/ * * @return boolean true/false in success/failure * @throws UnsupportedEncodingException */ static boolean createpub() throws UnsupportedEncodingException { post = new HttpPost(fqdn + "/.well-known/netinfproto/publish"); ContentBody url = new StringBody(niname); ContentBody msgid = new StringBody(Integer.toString(randomGenerator.nextInt(100000000))); ContentBody fullPut = new StringBody("yes"); ContentBody ext = new StringBody("no extension"); ContentBody bin = new FileBody(new File(filename)); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("octets", bin); reqEntity.addPart("URI", url); reqEntity.addPart("msgid", msgid); reqEntity.addPart("fullPut", fullPut); reqEntity.addPart("ext", ext); post.setEntity(reqEntity); return true; }
From source file:org.apache.sling.testing.tools.osgi.WebconsoleClient.java
/** Install a bundle using the Felix webconsole HTTP interface, with a specific start level */ public void installBundle(File f, boolean startBundle, int startLevel) throws Exception { // Setup request for Felix Webconsole bundle install final MultipartEntity entity = new MultipartEntity(); entity.addPart("action", new StringBody("install")); if (startBundle) { entity.addPart("bundlestart", new StringBody("true")); }/*from w w w . j a v a 2 s . c o m*/ entity.addPart("bundlefile", new FileBody(f)); if (startLevel > 0) { entity.addPart("bundlestartlevel", new StringBody(String.valueOf(startLevel))); log.info("Installing bundle {} at start level {}", f.getName(), startLevel); } else { log.info("Installing bundle {} at default start level", f.getName()); } // Console returns a 302 on success (and in a POST this // is not handled automatically as per HTTP spec) executor.execute(builder.buildPostRequest(CONSOLE_BUNDLES_PATH).withCredentials(username, password) .withEntity(entity)).assertStatus(302); }
From source file:org.opencastproject.remotetest.server.IngestRestEndpointTest.java
@Test public void testUploadClient() throws Exception { InputStream is = getClass().getResourceAsStream("/mp-test.zip"); InputStreamBody fileContent = new InputStreamBody(is, "mp-test.zip"); // create emptiy MediaPackage HttpPost postStart = new HttpPost(BASE_URL + "/ingest/addZippedMediaPackage"); MultipartEntity mpEntity = new MultipartEntity(); mpEntity.addPart("workflowDefinitionId", new StringBody("full")); mpEntity.addPart("userfile", fileContent); postStart.setEntity(mpEntity);/*from ww w . j ava2 s .c om*/ HttpResponse response = client.execute(postStart); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); }
From source file:com.qcloud.CloudClient.java
public String postfiles(String url, Map<String, String> header, Map<String, Object> body, byte[][] data, String[] pornFile) throws UnsupportedEncodingException, IOException { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("accept", "*/*"); httpPost.setHeader("user-agent", "qcloud-java-sdk"); if (header != null) { for (String key : header.keySet()) { httpPost.setHeader(key, header.get(key)); }/* w w w . j ava 2 s.c o m*/ } if (false == header.containsKey("Content-Type") || header.get("Content-Type").equals("multipart/form-data")) { MultipartEntity multipartEntity = new MultipartEntity(); if (body != null) { for (String key : body.keySet()) { multipartEntity.addPart(key, new StringBody(body.get(key).toString())); } } if (data != null) { for (int i = 0; i < data.length; i++) { ContentBody contentBody = new ByteArrayBody(data[i], pornFile[i]); multipartEntity.addPart("image[" + Integer.toString(i) + "]", contentBody); } } httpPost.setEntity(multipartEntity); } // HttpHost proxy = new HttpHost("127.0.0.1",8888); // mClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); HttpResponse httpResponse = mClient.execute(httpPost); int code = httpResponse.getStatusLine().getStatusCode(); return EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); }