Example usage for org.apache.http.protocol HTTP UTF_8

List of usage examples for org.apache.http.protocol HTTP UTF_8

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP UTF_8.

Prototype

String UTF_8

To view the source code for org.apache.http.protocol HTTP UTF_8.

Click Source Link

Usage

From source file:org.elasticsearch.querydoge.helper.HttpHelper.java

public String postAndReturnBody(String path, String message) throws ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(baseUrl + path);
    StringEntity entity = new StringEntity(message, HTTP.UTF_8);
    entity.setContentType("application/x-www-form-urlencoded");
    httpPost.setEntity(entity);/*from w  w  w .ja  v a2  s.co m*/

    System.out.println(httpPost);
    System.out.println(message);

    return httpclient.execute(httpPost, responseHandler);
}

From source file:com.tarsoft.openlibra.OpenLibraClient.java

public List<Book> getBooks(Criteria criteria) throws JSONException, MalformedURLException, IOException {

    this.criteria = criteria;

    HttpClient httpClient = new DefaultHttpClient();

    String getURL = getURLOpenLibra();

    Log.v(TAG, "URL: " + getURL);

    URI uri;//from  ww  w . j av a2 s .co m
    String data = null;
    try {
        uri = new URI(getURL);
        HttpGet method = new HttpGet(uri);
        HttpResponse response = httpClient.execute(method);

        HttpEntity resEntity = response.getEntity();

        //if exists, get it
        if (resEntity != null) {
            data = EntityUtils.toString(resEntity, HTTP.UTF_8);
        }

    } catch (Exception e) {
        e.printStackTrace();
        Log.v(TAG, "Error: " + e.getMessage() + " - " + e.getLocalizedMessage());
    }

    if (data != null) {
        //Delete initial "(" and final ");"
        data = data.substring(1, data.length() - 2);

        return parseData(data);
    } else {
        return null;
    }

}

From source file:org.addhen.smssync.net.SmsSyncHttpClient.java

/**
 * Upload SMS to a web service via HTTP POST
 * /*from   w ww .  j a v a  2s.co m*/
 * @param address
 * @throws MalformedURLException
 * @throws IOException
 * @return
 */
public static boolean postSmsToWebService(String url, HashMap<String, String> params, Context context) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {

        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("secret", params.get("secret")));
        nameValuePairs.add(new BasicNameValuePair("from", params.get("from")));
        nameValuePairs.add(new BasicNameValuePair("message", params.get("message")));
        nameValuePairs.add(new BasicNameValuePair("sent_timestamp", params.get("sent_timestamp")));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        if (response.getStatusLine().getStatusCode() == 200) {
            String resp = getText(response);
            boolean success = Util.extractPayloadJSON(resp);

            if (success) {
                // auto response message is enabled to be received from the
                // server.
                if (Prefrences.enableReplyFrmServer) {
                    Util.sendResponseFromServer(context, resp);
                }

                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }

    } catch (ClientProtocolException e) {
        return false;
    } catch (IOException e) {
        return false;
    }

}

From source file:li.barter.http.BlMultiPartRequest.java

@Override
protected Response<ResponseInfo> parseNetworkResponse(final NetworkResponse response) {

    final HttpResponseParser parser = new HttpResponseParser();
    try {/* w w  w. j  av  a2s  .c om*/
        final ResponseInfo responseInfo = parser.getSuccessResponse(mRequestId,
                new String(response.data, HTTP.UTF_8));

        if (responseInfo.success) {
            return Response.success(responseInfo, HttpHeaderParser.parseCacheHeaders(response));
        }

        return Response.error(new ParseError("Unable to parse and store data!"));
    } catch (final JSONException e) {
        return Response.error(new ParseError(e));
    } catch (final UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (final XmlPullParserException e) {
        return Response.error(new ParseError(e));
    } catch (final IOException e) {
        return Response.error(new ParseError(e));
    }
}

From source file:pingdesktop.Sample.HTTPhandlerSample.java

private void doPost() throws IOException {

    /* create the HTTP client and POST request */
    HttpPost httpPost = new HttpPost("http://xrozz.com/mlogg/api/user/login");

    /* add some request parameters for a form request */
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();

    nvps.add(new BasicNameValuePair("username", "pangeranweb"));
    nvps.add(new BasicNameValuePair("password", "anisnuzulan"));
    nvps.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    /* execute request */
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();

    /* process response */
    if (httpResponse.getStatusLine().getStatusCode() == 200) {
        String responseText = EntityUtils.toString(httpEntity);
        System.out.println(responseText);

    } else {/*  ww  w.j  a  v a2s . c om*/
        System.err.println("Invalid HTTP response: " + httpResponse.getStatusLine().getStatusCode());
    }

}

From source file:net.mandaria.radioreddit.tasks.SendExceptionEmailTask.java

@Override
protected Void doInBackground(Void... unused) {
    String url = "http://www.bryandenny.com/software/BugReport.aspx";

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("stacktrace", _stacktrace));
    nvps.add(new BasicNameValuePair("debug", _debug));
    nvps.add(new BasicNameValuePair("application", _application));
    try {/* w ww.j  a  va  2 s  . c om*/
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        httpClient.execute(httpPost);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.testmax.uri.ExecuteHttpRequest.java

public String handleHTTPPostUnit() throws Exception {
    String resp = null;/*from  w w  w . jav a2s.  c  o m*/
    boolean isItemIdReplaced = false;
    String replacedParam = "";
    System.out.println(this.url);
    HttpPost httpost = new HttpPost(this.url);
    //System.out.println(this.url);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    URLConfig urlConf = this.page.getURLConfig();

    Set<String> s = urlConf.getUrlParamset();
    for (String param : s) {
        replacedParam = urlConf.getUrlParamValue(param);
        System.out.println(urlConf.getUrlParamValue(param));
        nvps.add(new BasicNameValuePair(param, urlConf.getUrlParamValue(param)));
    }

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    //set the time for this HTTP resuest
    this.startRecording();
    long starttime = this.getCurrentTime();
    HttpResponse response = this.httpsclient.execute(httpost);
    HttpEntity entity = response.getEntity();
    // Measure the time passed between response
    long elaspedTime = this.getElaspedTime(starttime);
    this.stopRecording(response, elaspedTime);
    resp = this.getResponseBodyAsString(entity);
    System.out.println(resp);
    System.out.println("ElaspedTime: " + elaspedTime);
    WmLog.getCoreLogger().info("Response XML: " + resp);
    WmLog.getCoreLogger().info("ElaspedTime: " + elaspedTime);
    this.printRecording();
    this.printLog();
    if (this.getResponseStatus() == 200 && validateAssert(urlConf, resp)) {
        this.addThreadActionMonitor(this.action, true, elaspedTime);
    } else {
        this.addThreadActionMonitor(this.action, false, elaspedTime);
        WmLog.getCoreLogger().info("Input XML: " + replacedParam);
        WmLog.getCoreLogger().info("Response XML: " + resp);
    }
    this.closeEntity(entity);
    return (resp);

}

From source file:fr.pasteque.client.utils.URLTextGetter.java

public static void getText(final String url, final Map<String, String> getParams,
        final Map<String, String> postParams, final Handler h) {
    new Thread() {
        @Override//from  w w w.  j a  v  a  2s  .c o  m
        public void run() {
            try {
                String fullUrl = url;
                if (getParams != null && getParams.size() > 0) {
                    fullUrl += "?";
                    for (String param : getParams.keySet()) {
                        fullUrl += URLEncoder.encode(param, "utf-8") + "="
                                + URLEncoder.encode(getParams.get(param), "utf-8") + "&";
                    }
                }
                if (fullUrl.endsWith("&")) {
                    fullUrl = fullUrl.substring(0, fullUrl.length() - 1);
                }
                HttpClient client = new DefaultHttpClient();
                HttpResponse response = null;
                if (postParams == null) {
                    HttpGet req = new HttpGet(fullUrl);
                    response = client.execute(req);
                } else {
                    HttpPost req = new HttpPost(fullUrl);
                    List<NameValuePair> args = new ArrayList<NameValuePair>();
                    for (String key : postParams.keySet()) {
                        String value = postParams.get(key);
                        args.add(new BasicNameValuePair(key, value));
                    }
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(args, HTTP.UTF_8);
                    req.setEntity(entity);
                    response = client.execute(req);
                }
                int status = response.getStatusLine().getStatusCode();
                if (status == HttpStatus.SC_OK) {
                    // Get http response
                    String content = "";
                    try {
                        final int size = 10240;
                        ByteArrayOutputStream bos = new ByteArrayOutputStream(size);
                        byte[] buffer = new byte[size];
                        BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent(),
                                size);
                        int read = bis.read(buffer, 0, size);
                        while (read != -1) {
                            bos.write(buffer, 0, read);
                            read = bis.read(buffer, 0, size);
                        }
                        content = new String(bos.toByteArray());
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    if (h != null) {
                        Message m = h.obtainMessage();
                        m.what = SUCCESS;
                        m.obj = content;
                        m.sendToTarget();
                    }
                } else {
                    if (h != null) {
                        Message m = h.obtainMessage();
                        m.what = STATUS_NOK;
                        m.obj = new Integer(status);
                        m.sendToTarget();
                    }
                }
            } catch (IOException e) {
                if (h != null) {
                    Message m = h.obtainMessage();
                    m.what = ERROR;
                    m.obj = e;
                    m.sendToTarget();
                }
            }
        }
    }.start();
}

From source file:com.zekke.services.http.JsonRequestBuilder.java

@Override
public JsonRequestBuilder setDefaultHeaders() {
    super.setDefaultHeaders();
    headers.add(new BasicHeader(HeaderField.ACCEPT.getValue(), ContentType.APPLICATION_JSON.getValue()));
    headers.add(new BasicHeader(HeaderField.ACCEPT_CHARSET.getValue(), HTTP.UTF_8));
    return this;
}