List of usage examples for org.apache.http.entity.mime.content StringBody StringBody
public StringBody(final String text) throws UnsupportedEncodingException
From source file:com.globalsight.everest.tda.TdaHelper.java
public String loginCheck(String hostName, String userName, String password) { int timeoutConnection = 8000; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters); String loginUrl = new String(); String errorInfo = new String(); hostName = hostName.trim();/* w w w. j a v a 2 s. c om*/ userName = userName.trim(); if (hostName.indexOf("http://") < 0) { loginUrl = "http://" + hostName; } else { loginUrl = hostName; } if (hostName.lastIndexOf("/") == (hostName.length() - 1)) { loginUrl = loginUrl + "auth_key.json?action=login"; } else { loginUrl = loginUrl + "/auth_key.json?action=login"; } try { HttpPost httpost = new HttpPost(loginUrl); MultipartEntity reqEntity = new MultipartEntity(); StringBody nameBody = new StringBody(userName); StringBody passwordBody = new StringBody(password); StringBody appKeyBody = new StringBody(appKey); reqEntity.addPart("auth_username", nameBody); reqEntity.addPart("auth_password", passwordBody); reqEntity.addPart("auth_app_key", appKeyBody); httpost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httpost); StatusLine sl = response.getStatusLine(); if (sl.getStatusCode() == 404) { errorInfo = "The TDA URL is not correct."; } else if (sl.getStatusCode() == 401) { errorInfo = "The username and password given are not a valid TDA login."; } else if (sl.getStatusCode() == 201) { errorInfo = "ture"; } else { errorInfo = "The TDA configuration is not correct!"; } } catch (Exception e) { s_logger.info("Can not connect TDA server:" + e.getMessage()); errorInfo = "Can not connect TDA server."; } return errorInfo; }
From source file:org.apache.sling.testing.tools.sling.SlingClient.java
/** Create a node at specified path, with optional properties * @param path Used in POST request to Sling server * @param properties If not null, properties are added to the created node * @return The actual path of the node that was created *//* w w w .j a v a 2 s . com*/ public String createNode(String path, Map<String, Object> properties) throws UnsupportedEncodingException, IOException { String actualPath = null; final MultipartEntity entity = new MultipartEntity(); // Add Sling POST options entity.addPart(":redirect", new StringBody("*")); entity.addPart(":displayExtension", new StringBody("")); // Add user properties if (properties != null) { for (Map.Entry<String, Object> e : properties.entrySet()) { entity.addPart(e.getKey(), new StringBody(e.getValue().toString())); } } final HttpResponse response = executor .execute(builder.buildPostRequest(path).withEntity(entity).withCredentials(username, password)) .assertStatus(302).getResponse(); final Header location = response.getFirstHeader(LOCATION_HEADER); assertNotNull("Expecting " + LOCATION_HEADER + " in response", location); actualPath = locationToPath(location.getValue()); return actualPath; }
From source file:jp.canetrash.maven.plugin.bijint.BujintMojo.java
/** * @param image//from w ww . j a v a 2s. co m * @return * @throws IOException * @throws ClientProtocolException * @throws UnsupportedEncodingException */ private String getAsciiArt(BufferedImage image) throws IOException, ClientProtocolException, UnsupportedEncodingException { File tmpfile = File.createTempFile("bjint_", ".jpg"); ImageIO.write(image, "jpg", tmpfile); // http://picascii.com/???? // ????? DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://picascii.com/"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); entity.consumeContent(); // ??? HttpPost httppost = new HttpPost("http://picascii.com/upload.php"); //HttpPost httppost = buildDefaultHttpMessage(new HttpPost("http://localhost:8080/sa-struts-tutorial/upload/")); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); FileBody bin = new FileBody(tmpfile, "image/jpeg"); reqEntity.addPart("imageupload", bin); reqEntity.addPart("MAX_FILE_SIZE", new StringBody("1000000")); reqEntity.addPart("url", new StringBody("")); reqEntity.addPart("quality", new StringBody("3")); reqEntity.addPart("size", new StringBody("1")); httppost.setEntity(reqEntity); response = httpclient.execute(httppost); String responseHtml = IOUtils.toString(response.getEntity().getContent()); httpclient.getConnectionManager().shutdown(); // tmpFile? tmpfile.delete(); return bringOutAsciiArtString(responseHtml); }
From source file:hu.sztaki.lpds.portal.util.stream.HttpClient.java
/** * File upload/* www . j av a 2s.c om*/ * @param pFile file to be uploaded * @param uploadName upload naem * @param pValue parameters used during the connection * @return http answer code * @throws java.lang.Exception communication error */ public int fileUpload(File pFile, String uploadName, Hashtable pValue) throws Exception { int res = 0; MultipartEntity reqEntity = new MultipartEntity(); // file parameter if (uploadName != null) { FileBody bin = new FileBody(pFile); reqEntity.addPart(uploadName, bin); } //text parameters Enumeration<String> enm = pValue.keys(); String key; while (enm.hasMoreElements()) { key = enm.nextElement(); reqEntity.addPart(key, new StringBody("" + pValue.get(key))); } httpPost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httpPost); HttpEntity resEntity = response.getEntity(); res = response.getStatusLine().getStatusCode(); close(); return res; }
From source file:edu.uah.itsc.aws.RubyClient.java
public void postFile(byte[] image) throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); // HttpPost httppost = new // HttpPost("http://ec2-107-21-179-173.compute-1.amazonaws.com:3000/posts"); HttpPost httppost = new HttpPost(publicURL); ContentBody cb = new ByteArrayBody(image, "text/plain; charset=utf8", fname); // ContentBody cb = new InputStreamBody(new ByteArrayInputStream(image), // "image/jpg", "icon.jpg"); MultipartEntity mpentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); // mpentity.addPart(cb.); // mpentity.addPart("utf8", new // StringBody((Character.toString('\u2713')))); mpentity.addPart("post[photo]", cb); mpentity.addPart("post[content]", new StringBody(desc)); // mpentity.addPart("post[filename]", new StringBody( fname)); mpentity.addPart("post[title]", new StringBody(title)); mpentity.addPart("post[bucket]", new StringBody(bucket)); mpentity.addPart("post[user]", new StringBody(User.username)); mpentity.addPart("post[folder]", new StringBody(folder)); mpentity.addPart("commit", new StringBody("Create Post")); httppost.setEntity(mpentity);//from ww w. ja v a 2 s.c o m String response = EntityUtils.toString(httpclient.execute(httppost).getEntity(), "UTF-8"); System.out.println(response); }
From source file:com.github.mhendred.face4j.CopyOfResponderImpl.java
/** * @see {@link Responder#doPost(File, URI, List)} *///w ww . ja v a 2s. com public String doPost(final File file, final URI uri, final List<NameValuePair> params) throws FaceClientException, FaceServerException { try { final MultipartEntity entity = new MultipartEntity(); entity.addPart("image", new FileBody(file)); try { for (NameValuePair nvp : params) { entity.addPart(nvp.getName(), new StringBody(nvp.getValue())); } } catch (UnsupportedEncodingException uee) { throw new FaceClientException(uee); } postMethod.setURI(uri); postMethod.setEntity(entity); final long start = System.currentTimeMillis(); final HttpResponse response = httpClient.execute(postMethod); return checkResponse(response); } catch (IOException ioe) { throw new FaceClientException(ioe); } }
From source file:net.asplode.tumblr.Post.java
/** * @param generator// w w w. j a v a 2s . c o m * A short description of the application making the request. Max * 64 characters. * @throws UnsupportedEncodingException */ public void setGenerator(String generator) throws UnsupportedEncodingException { entity.addPart("generator", new StringBody(generator)); }
From source file:com.captchatrader.CaptchaTrader.java
/** * Submit a CAPTCHA already hosted on an existing website. * /*from w w w . j a va2s . c o m*/ * @param uri * The URI of the CAPTCHA image. * @return The decoded CAPTCHA. * @throws Any * exceptions sent by the server. */ public ResolvedCaptcha submit(URI uri) throws CaptchaTraderException, IOException { final URI requestUri = apiURI.resolve("submit"); final HttpPost request = new HttpPost(requestUri); final MultipartEntity entity = new MultipartEntity(); entity.addPart("api_key", new StringBody(applicationKey)); entity.addPart("username", new StringBody(username)); entity.addPart("password", new StringBody(password)); entity.addPart("value", new StringBody(uri.toString())); request.setEntity(entity); final List<Object> response = validate(execute(request)); return new ResolvedCaptcha(this, ((Long) response.get(0)).toString(), (String) response.get(1)); }
From source file:at.univie.sensorium.extinterfaces.HTTPSUploader.java
private String uploadFiles(List<File> files) { String result = ""; try {//from ww w . j a v a 2 s . c o m if (URLUtil.isValidUrl(posturl)) { HttpClient httpclient = getNewHttpClient(); HttpPost httppost = new HttpPost(posturl); MultipartEntity mpEntity = new MultipartEntity(); // MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); mpEntity.addPart("username", new StringBody(username)); mpEntity.addPart("password", new StringBody(password)); for (File file : files) { Log.d(SensorRegistry.TAG, "preparing " + file.getName() + " for upload"); ContentBody cbFile = new FileBody(file, "application/json"); mpEntity.addPart(file.toString(), cbFile); } httppost.addHeader("username", username); httppost.addHeader("password", password); httppost.setEntity(mpEntity); HttpResponse response = httpclient.execute(httppost); String reply; InputStream in = response.getEntity().getContent(); StringBuilder sb = new StringBuilder(); try { int chr; while ((chr = in.read()) != -1) { sb.append((char) chr); } reply = sb.toString(); } finally { in.close(); } result = response.getStatusLine().toString(); Log.d(SensorRegistry.TAG, "Http upload completed with response: " + result + " " + reply); } else { result = "URL invalid"; Log.d(SensorRegistry.TAG, "Invalid http upload url, aborting."); } } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (FileNotFoundException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (ClientProtocolException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (IOException e) { result = "upload failed due to timeout"; StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } return result; }
From source file:com.phodev.http.tools.RequestEntity.java
/** * POST//ww w . j a v a 2 s . co m * * <pre> * RequestMethod{@link ConnectionHelper.RequestMethod#POST_WITH_FILE}}? * </pre> * * @param postValues * @param files * @param charset */ public RequestEntity setPostEntitiy(List<NameValuePair> postValues, String charset, Map<String, File> files) { Charset c = null; try { c = Charset.forName(charset); Charset.defaultCharset(); } catch (Exception e) { c = null; } MultipartEntity entity; HttpMultipartMode mode = HttpMultipartMode.BROWSER_COMPATIBLE; if (c == null) { entity = new MultipartEntity(mode); } else { entity = new MultipartEntity(mode, null, c); } postEntity = entity; if (postValues != null) { for (NameValuePair v : postValues) { try { entity.addPart(v.getName(), new StringBody(v.getValue())); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } if (files != null) { Iterator<Entry<String, File>> iterator = files.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, File> entry = iterator.next(); entity.addPart(entry.getKey(), new FileBody(entry.getValue())); } } return this; }