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:de.escidoc.core.test.sm.ScopeTestBase.java

/**
 * Test retrieving an Scope from the mock framework.
 *
 * @param id The id of the Scope.//from   w  w w  .  java2s . c o  m
 * @return The retrieved Scope.
 * @throws Exception If anything fails.
 */
public String retrieve(final String id) throws Exception {

    Object result = getScopeClient().retrieve(id);
    String xmlResult = null;
    if (result instanceof HttpResponse) {
        HttpResponse httpRes = (HttpResponse) result;
        assertHttpStatusOfMethod("", httpRes);
        xmlResult = EntityUtils.toString(httpRes.getEntity(), HTTP.UTF_8);
    } else if (result instanceof String) {
        xmlResult = (String) result;
    }
    return xmlResult;
}

From source file:com.sumologic.log4j.SumoLogicAppender.java

private void sendToSumo(String log) {
    HttpPost post = null;/*from w  ww. j a va  2 s .co m*/
    try {
        post = new HttpPost(url);
        post.setEntity(new StringEntity(log, HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            LogLog.warn(String.format("Received HTTP error from Sumo Service: %d", statusCode));
        }
        //need to consume the body if you want to re-use the connection.
        EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        LogLog.warn("Could not send log to Sumo Logic", e);
        try {
            post.abort();
        } catch (Exception ignore) {
        }
    }
}

From source file:com.phonty.improved.Contacts.java

public boolean delete(String contact_id) {
    StringBuilder builder = new StringBuilder();
    try {/* w w  w.ja  va  2  s . c  o m*/
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("id", contact_id));

        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = client.execute(httppost);

        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                VALUE = Parse(line);
            }
        } else {
            this.VALUE = "0.0";
        }
    } catch (ClientProtocolException e) {
        this.VALUE = "Protocol exception";
        e.printStackTrace();
    } catch (IOException e) {
        this.VALUE = "I.O. Exception";
        e.printStackTrace();
    }
    if (VALUE.equals("OK"))
        return true;
    else
        return false;
}

From source file:com.phonty.improved.Sms.java

public boolean send(String phone, String message) {
    StringBuilder builder = new StringBuilder();
    try {//from   w w  w  . ja v  a2 s.com
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("phone", phone));
        nvps.add(new BasicNameValuePair("message", message));

        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = client.execute(httppost);

        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                STATUS = Parse(line);
            }
        } else {
            this.STATUS = "0.0";
        }
    } catch (ClientProtocolException e) {
        this.STATUS = "SE";
        e.printStackTrace();
    } catch (IOException e) {
        this.STATUS = "SE";
        e.printStackTrace();
    }

    if (STATUS.equals("OK"))
        return true;
    return false;
}

From source file:com.isotrol.impe3.connectors.httpclient.AbstractHttpClientConnector.java

protected Response doPost(String url, Multimap<String, String> params, boolean bytes) {
    Response res;//from  w ww  .j  av  a  2s  .c  om
    final HttpPost httppost = new HttpPost(url);
    final List<NameValuePair> nvps = TOLIST.apply(params);

    try {
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        final HttpResponse response = httpclient.execute(httppost);

        res = http(response, bytes);
    } catch (UnsupportedEncodingException e) {
        res = Response.code(400);
        logger.warn("UnsupportedEncodingException POST {}", url);
        logger.trace("Error trace, ", e);
    } catch (ClientProtocolException e) {
        res = Response.code(400);
        logger.warn("ClientProtocolException POST {}", url);
        logger.trace("Error trace, ", e);
    } catch (IOException e) {
        res = Response.code(400);
        logger.warn("IOException POST {}", url);
        logger.trace("Error trace, ", e);
    } finally {
        httpclient.getConnectionManager().closeExpiredConnections();
        httpclient.getConnectionManager().closeIdleConnections(timeout, TimeUnit.SECONDS);
    }

    return res;

}

From source file:com.spokenpic.net.RestClient.java

protected RestResult returnResponse(HttpResponse httpResponse) {
    try {//from   ww  w .  j a v  a  2  s  . c o  m
        //mClient.close();
        RestResult result = new RestResult();
        result.httpCode = httpResponse.getStatusLine().getStatusCode();
        if (httpResponse.getEntity() != null) {
            result.payload = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
            //Log.d("HTTP", "Got response: " + result.payload);
        }
        if (result.httpCode < 200 || result.httpCode > 299) {
            result.ok = false;
            if (result.payload == null || result.payload.length() == 0) {
                if (result.httpCode >= 400 && result.httpCode < 500) {
                    result.payload = "Not found";
                } else {
                    result.payload = "Server error";
                }
            }
            App.DEBUG("HTTP Status: " + result.httpCode + " Body: "
                    + result.payload.substring(0, Math.min(50, result.payload.length()) - 1));
            return result;
        } else {
            //App.DEBUG(result.payload);
            result.ok = true;
        }
        return result;
    } catch (Exception e) {
        Log.d("RestClient", "Error returnResponse " + e.toString());
        return errorResponse("Fatal error in response");
    }
}

From source file:com.sumologic.logback.SumoLogicAppender.java

private void sendToSumo(String data) {
    HttpPost post = null;//from  ww  w .j a v  a 2  s. co  m
    try {
        post = new HttpPost(url);
        post.setEntity(new StringEntity(data, HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            log.warn("Received HTTP error from Sumo Service: {}", statusCode);
        }
        //need to consume the body if you want to re-use the connection.
        EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        log.warn("Could not send log to Sumo Logic", e);
        try {
            post.abort();
        } catch (Exception ignore) {
        }
    }
}

From source file:com.intel.iotkitlib.http.HttpDeleteTask.java

public CloudResponse doSync(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    try {//w  w w .java 2 s  .c om
        HttpContext localContext = new BasicHttpContext();
        HttpDelete httpDelete = new HttpDelete(url);
        //adding headers one by one
        for (NameValuePair nvp : headerList) {
            httpDelete.addHeader(nvp.getName(), nvp.getValue());
        }
        if (debug) {
            Log.e(TAG, "URI is : " + httpDelete.getURI());
            Header[] headers = httpDelete.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                Log.e(TAG, "Header " + i + " is :" + headers[i].getName() + ":" + headers[i].getValue());
            }
        }
        HttpResponse response = httpClient.execute(httpDelete, localContext);
        if (response != null && response.getStatusLine() != null) {
            if (debug)
                Log.d(TAG, "response: " + response.getStatusLine().getStatusCode());
        }
        HttpEntity responseEntity = response != null ? response.getEntity() : null;
        StringBuilder builder = new StringBuilder();
        if (responseEntity != null) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(responseEntity.getContent(), HTTP.UTF_8));
            for (String line = null; (line = reader.readLine()) != null;) {
                builder.append(line).append("\n");
            }
            if (debug)
                Log.d(TAG, "Response received is :" + builder.toString());
        }
        CloudResponse cloudResponse = new CloudResponse();
        if (response != null) {
            cloudResponse.code = response.getStatusLine().getStatusCode();
        }
        cloudResponse.response = builder.toString();
        return cloudResponse;
    } catch (java.net.ConnectException cEx) {
        Log.e(TAG, cEx.getMessage());
        return new CloudResponse(false, cEx.getMessage());
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        return new CloudResponse(false, e.getMessage());
    }
}

From source file:net.reichholf.dreamdroid.helpers.SimpleHttpClient.java

/**
 * @param uri/*from ww w . j  a va 2  s.c om*/
 * @param parameters
 * @return
 */
public String buildFileStreamUrl(String uri, List<NameValuePair> parameters) {
    String parms = URLEncodedUtils.format(parameters, HTTP.UTF_8).replace("+", "%20");
    String fileAuthString = "";
    if (mProfile.isFileLogin())
        fileAuthString = mProfile.getUser() + ":" + mProfile.getPass() + "@";

    String url = mFilePrefix + fileAuthString + mProfile.getStreamHost() + ":" + mProfile.getFilePortString()
            + uri + parms;
    return url;
}

From source file:com.nexmo.sdk.core.client.Client.java

/**
 * Construct a Nexmo Url instance./*from  ww w .  ja  v  a 2  s.  c om*/
 *
 * @param requestParams The necessary http params.
 * @param methodName The method name inside the API call.
 *
 * @throws MalformedURLException if an error occurs while opening the connection.
 */
private static URL constructUrlGetConnection(Map<String, String> requestParams, final String methodName,
        final String host) throws MalformedURLException {
    List<NameValuePair> getParams = new ArrayList<>();
    for (Map.Entry<String, String> entry : requestParams.entrySet()) {
        getParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }

    String paramString = URLEncodedUtils.format(getParams, HTTP.UTF_8);
    return new URL(host + methodName + paramString);
}