Example usage for org.apache.http.entity.mime.content StringBody StringBody

List of usage examples for org.apache.http.entity.mime.content StringBody StringBody

Introduction

In this page you can find the example usage for org.apache.http.entity.mime.content StringBody StringBody.

Prototype

public StringBody(final String text) throws UnsupportedEncodingException 

Source Link

Usage

From source file:com.captchatrader.CaptchaTrader.java

/**
 * Responds if an CAPTCHA wes correctly answered or not
 * /*from   ww w .  j a  v  a  2 s . c om*/
 * @param captcha
 *            the CAPTCHA object
 * @param state
 *            <code>true</code> if the CAPTCHA was correctly resolved
 * @throws CaptchaTraderException
 *             any of the possible errors
 */
public void respond(ResolvedCaptcha captcha, boolean state) throws CaptchaTraderException, IOException {
    final URI requestUri = apiURI.resolve("respond");
    final HttpPost request = new HttpPost(requestUri);
    final MultipartEntity entity = new MultipartEntity();

    entity.addPart("is_correct", new StringBody(state ? "1" : "0"));
    entity.addPart("password", new StringBody(password));
    entity.addPart("ticket", new StringBody(captcha.getID()));
    entity.addPart("username", new StringBody(username));

    request.setEntity(entity);
    validate(execute(request));
}

From source file:info.guardianproject.net.http.HttpManager.java

public static String uploadFile(String serviceEndpoint, Properties properties, String fileParam, String file)
        throws Exception {

    HttpClient httpClient = new DefaultHttpClient();

    HttpPost request = new HttpPost(serviceEndpoint);
    MultipartEntity entity = new MultipartEntity();

    Iterator<Map.Entry<Object, Object>> i = properties.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) i.next();
        String key = (String) entry.getKey();
        String val = (String) entry.getValue();
        entity.addPart(key, new StringBody(val));

    }//www . j  a  va  2 s. c  om
    File upload = new File(file);
    Log.i("httpman", "upload file (" + upload.getAbsolutePath() + ") size=" + upload.length());

    entity.addPart(fileParam, new FileBody(upload));
    request.setEntity(entity);

    HttpResponse response = httpClient.execute(request);
    int status = response.getStatusLine().getStatusCode();

    if (status != HttpStatus.SC_OK) {

    } else {

    }

    return response.toString();

}

From source file:net.kidlogger.kidlogger.SendTestReport.java

private void sendPOST() {
    File fs = getFile();/*w  w  w.j a  va  2 s . c om*/
    Date now = new Date();
    String fileDate = String.format("%td/%tm/%tY %tT", now, now, now, now);
    String devField = Settings.getDeviceField(context);
    if (devField.equals("undefined") || devField.equals(""))
        return;
    try {
        HttpClient client = new DefaultHttpClient();
        String postUrl = context.getString(R.string.upload_link);
        //String postUrl = "http://10.0.2.2/denwer/";
        HttpPost post = new HttpPost(postUrl);
        FileBody bin = new FileBody(fs, "text/html", fs.getName());
        StringBody sb1 = new StringBody(devField);
        StringBody sb2 = new StringBody("HTML");
        StringBody sb3 = new StringBody("Andriod_2_2");
        StringBody sb4 = new StringBody("1.0");
        StringBody sb5 = new StringBody(fileDate);

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.STRICT);
        reqEntity.addPart("file", bin);
        reqEntity.addPart("device", sb1);
        reqEntity.addPart("content", sb2);
        reqEntity.addPart("client-ver", sb3);
        reqEntity.addPart("app-ver", sb4);
        reqEntity.addPart("client-date-time", sb5);

        post.setEntity(reqEntity);
        HttpResponse response = client.execute(post);
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            String pStatus = EntityUtils.toString(resEntity);
            if (pStatus.equalsIgnoreCase("Ok")) {
                fs.delete();
                testRes.setText("Response: " + pStatus);
            } else {
                //Log.i("sendPOST", pStatus);
            }
            testRes.setText("Response: " + pStatus);
            //Log.i("sendPOST", "Response: " + pStatus);
        }
    } catch (Exception e) {
        //Log.i("sendPOST", e.toString());
    }
}

From source file:com.impetus.ankush.agent.action.impl.UploadHandler.java

/**
 * Method that builds the multi-part form data request.
 * //w ww.  j a v  a  2 s .c o m
 * @param urlString
 *            the urlString to which the file needs to be uploaded
 * @param file
 *            the actual file instance that needs to be uploaded
 * @param fileName
 *            name of the file, just to show how to add the usual form
 *            parameters
 * @param fileDescription
 *            some description for the file, just to show how to add the
 *            usual form parameters
 * @return server response as <code>String</code>
 */
public String executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) {

    HttpPost postRequest = new HttpPost(urlString);
    try {

        MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        // The usual form parameters can be added this way
        multiPartEntity.addPart("fileDescription",
                new StringBody(fileDescription != null ? fileDescription : ""));
        multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName()));

        /*
         * Need to construct a FileBody with the file that needs to be
         * attached and specify the mime type of the file. Add the fileBody
         * to the request as an another part. This part will be considered
         * as file part and the rest of them as usual form-data parts
         */
        FileBody fileBody = new FileBody(file, "application/octect-stream");
        multiPartEntity.addPart("file", fileBody);

        postRequest.setEntity(multiPartEntity);

    } catch (Exception ex) {
        LOGGER.error(ex.getMessage(), ex);
        return null;
    }

    return executeRequest(postRequest);
}

From source file:com.shafiq.myfeedle.core.PhotoUploadService.java

private void start(Intent intent) {
    if (intent != null) {
        String action = intent.getAction();
        if (Myfeedle.ACTION_UPLOAD.equals(action)) {
            if (intent.hasExtra(Accounts.TOKEN) && intent.hasExtra(Statuses.MESSAGE)
                    && intent.hasExtra(Widgets.INSTANT_UPLOAD)) {
                String place = null;
                if (intent.hasExtra(Splace))
                    place = intent.getStringExtra(Splace);
                String tags = null;
                if (intent.hasExtra(Stags))
                    tags = intent.getStringExtra(Stags);
                // upload a photo
                Notification notification = new Notification(R.drawable.notification, "uploading photo",
                        System.currentTimeMillis());
                notification.setLatestEventInfo(getBaseContext(), "photo upload", "uploading",
                        PendingIntent.getActivity(PhotoUploadService.this, 0,
                                (Myfeedle.getPackageIntent(PhotoUploadService.this, About.class)), 0));
                ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFY_ID,
                        notification);/*www  . ja  v  a2  s . com*/
                (new AsyncTask<String, Void, String>() {

                    @Override
                    protected String doInBackground(String... params) {
                        String response = null;
                        if (params.length > 2) {
                            Log.d(TAG, "upload file: " + params[2]);
                            HttpPost httpPost = new HttpPost(String.format(FACEBOOK_PHOTOS, FACEBOOK_BASE_URL,
                                    Saccess_token, mMyfeedleCrypto.Decrypt(params[0])));
                            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                            File file = new File(params[2]);
                            ContentBody fileBody = new FileBody(file);
                            entity.addPart(Ssource, fileBody);
                            HttpClient httpClient = MyfeedleHttpClient
                                    .getThreadSafeClient(getApplicationContext());
                            try {
                                entity.addPart(Smessage, new StringBody(params[1]));
                                if (params[3] != null)
                                    entity.addPart(Splace, new StringBody(params[3]));
                                if (params[4] != null)
                                    entity.addPart(Stags, new StringBody(params[4]));
                                httpPost.setEntity(entity);
                                response = MyfeedleHttpClient.httpResponse(httpClient, httpPost);
                            } catch (UnsupportedEncodingException e) {
                                Log.e(TAG, e.toString());
                            }
                        }
                        return response;
                    }

                    @Override
                    protected void onPostExecute(String response) {
                        // notify photo success
                        String message = getString(response != null ? R.string.success : R.string.failure);
                        Notification notification = new Notification(R.drawable.notification,
                                "photo upload " + message, System.currentTimeMillis());
                        notification.setLatestEventInfo(getBaseContext(), "photo upload", message,
                                PendingIntent.getActivity(PhotoUploadService.this, 0,
                                        (Myfeedle.getPackageIntent(PhotoUploadService.this, About.class)), 0));
                        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFY_ID,
                                notification);
                        stopSelfResult(mStartId);
                    }

                }).execute(intent.getStringExtra(Accounts.TOKEN), intent.getStringExtra(Statuses.MESSAGE),
                        intent.getStringExtra(Widgets.INSTANT_UPLOAD), place, tags);
            }
        }
    }
}

From source file:com.openideals.android.net.HttpManager.java

public static String uploadFile(String serviceEndpoint, Properties properties, String fileParam, String file)
        throws Exception {

    HttpClient httpClient = new DefaultHttpClient();

    HttpPost request = new HttpPost(serviceEndpoint);
    MultipartEntity entity = new MultipartEntity();

    Iterator<Map.Entry<Object, Object>> i = properties.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) i.next();
        String key = (String) entry.getKey();
        String val = (String) entry.getValue();
        entity.addPart(key, new StringBody(val));
    }/* w  w  w .  j a v  a2s  .  c  om*/
    File upload = new File(file);
    Log.i("httpman", "upload file (" + upload.getAbsolutePath() + ") size=" + upload.length());

    entity.addPart(fileParam, new FileBody(upload));
    request.setEntity(entity);

    HttpResponse response = httpClient.execute(request);
    int status = response.getStatusLine().getStatusCode();

    if (status != HttpStatus.SC_OK) {

    } else {

    }

    return response.toString();

}

From source file:net.asplode.tumblr.Post.java

/**
 * @param slug/*w  ww.  j  a  va2s.c o m*/
 *            Custom string to appear in the post's URL
 * @throws UnsupportedEncodingException
 */
public void setSlug(String slug) throws UnsupportedEncodingException {
    entity.addPart("slug", new StringBody(slug));
}

From source file:com.wialon.remote.ApacheSdkHttpClient.java

@Override
public void postFile(String url, Map<String, String> params, Callback callback, int timeout, File file) {
    try {//from w ww.  ja  v  a2s  .  co  m
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity multipartEntity = new MultipartEntity();
        if (params != null)
            for (Map.Entry<String, String> entry : params.entrySet())
                multipartEntity.addPart(entry.getKey(), new StringBody(entry.getValue()));
        multipartEntity.addPart("file", new FileBody(file));
        httpPost.setEntity(multipartEntity);
        sendRequest(getHttpClient(timeout), httpPost, callback);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        callback.error = e;
        callback.done(null);
    }
}