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 putAndReturnBody(String path, String message) throws ClientProtocolException, IOException {
    HttpPut httpPut = new HttpPut(baseUrl + path);
    StringEntity entity = new StringEntity(message, HTTP.UTF_8);
    entity.setContentType("application/x-www-form-urlencoded");
    httpPut.setEntity(entity);/*from ww  w  .  j a  v  a2 s. c  om*/

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

    return httpclient.execute(httpPut, responseHandler);
}

From source file:org.dataconservancy.archive.impl.fcrepo.ri.RIClient.java

private InputStream executeQuery(String sparql, boolean flush) {
    HttpPost post = new HttpPost(riEndpoint);
    List<NameValuePair> args = new ArrayList<NameValuePair>();
    if (flush) {/*from   w  ww  .j  a  v  a  2 s .co m*/
        args.add(new BasicNameValuePair("flush", "true"));
    }
    args.add(new BasicNameValuePair("type", "tuples"));
    args.add(new BasicNameValuePair("lang", "sparql"));
    args.add(new BasicNameValuePair("format", "Simple"));
    args.add(new BasicNameValuePair("stream", "on"));
    args.add(new BasicNameValuePair("query", sparql));
    try {
        post.setEntity(new UrlEncodedFormEntity(args, HTTP.UTF_8));
    } catch (UnsupportedEncodingException wontHappen) {
        throw new RuntimeException(wontHappen);
    }
    try {
        HttpResponse response = httpClient.execute(post);
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode != 200) {
            throw new RuntimeException(
                    "Fedora Resource Index query " + "returned an unexpected HTTP response code: "
                            + responseCode + ". Consult Fedora Server log for " + "details.");
        }
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            return new ByteArrayInputStream(new byte[0]);
        } else {
            return entity.getContent();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:hu.sztaki.lpds.portal.util.stream.HttpClient.java

/**
 * Getting the stream// ww w. j  a  v a  2 s.c o m
 * @param pValue list of the parameters used during the connection
 * @return  datastream
 * @throws java.io.IOException communication error
 */
public InputStream getStream(Hashtable<String, String> pValue) throws IOException {
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    Enumeration<String> enm = pValue.keys();
    String key;
    while (enm.hasMoreElements()) {
        key = enm.nextElement();
        nvps.add(new BasicNameValuePair(key, pValue.get(key)));
    }
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    HttpResponse response = httpclient.execute(httpPost);
    HttpEntity entity = response.getEntity();
    return entity.getContent();
}

From source file:org.cm.podd.report.util.RequestDataUtil.java

public static ResponseObject post(String path, String query, String json, String token) {
    JSONObject jsonObj = null;//from   w w  w.  j a va  2 s  . co m
    int statusCode = 0;

    //SharedPreferences settings = PoddApplication.getAppContext().getSharedPreferences("PoddPrefsFile", 0);
    String serverUrl = settings.getString("serverUrl", BuildConfig.SERVER_URL);
    String reqUrl = "";

    if (path.contains("http://") || path.contains("https://")) {
        reqUrl = String.format("%s%s", path, query == null ? "" : "?" + query);
    } else {
        reqUrl = String.format("%s%s%s", serverUrl, path, query == null ? "" : "?" + query);
    }

    Log.i(TAG, "submit url=" + reqUrl);
    Log.i(TAG, "post data=" + json);

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpClient client = new DefaultHttpClient(params);

    try {
        HttpPost post = new HttpPost(reqUrl);
        post.setHeader("Content-Type", "application/json");
        if (token != null) {
            post.setHeader("Authorization", "Token " + token);
        }
        post.setEntity(new StringEntity(json, HTTP.UTF_8));

        HttpResponse response;
        response = client.execute(post);
        HttpEntity entity = response.getEntity();

        // Detect server complaints
        statusCode = response.getStatusLine().getStatusCode();
        Log.v(TAG, "status code=" + statusCode);

        if (statusCode == HttpURLConnection.HTTP_OK || statusCode == HttpURLConnection.HTTP_CREATED
                || statusCode == HttpURLConnection.HTTP_BAD_REQUEST) {
            InputStream in = entity.getContent();
            String resp = FileUtil.convertInputStreamToString(in);

            jsonObj = new JSONObject(resp);
            entity.consumeContent();
        }

    } catch (ClientProtocolException e) {
        Log.e(TAG, "error post data", e);
    } catch (IOException e) {
        Log.e(TAG, "Can't connect server", e);
    } catch (JSONException e) {
        Log.e(TAG, "error convert json", e);
    } finally {
        client.getConnectionManager().shutdown();
    }
    return new ResponseObject(statusCode, jsonObj);
}

From source file:lv.vizzual.numuri.service.http.HttpProvider.java

private void processPost(Request req) throws IOException {
    HttpPost post = null;/*w  ww  .ja v  a 2  s  .  c  o  m*/
    String result = null;
    HttpResponse response = null;

    try {
        post = new HttpPost(new URI(req.getUrl()));
        for (NameValuePair nv : req.getHeaders()) {
            post.setHeader(nv.getName(), nv.getValue());
        }

        post.setEntity(new UrlEncodedFormEntity(req.getData(), HTTP.UTF_8));
        response = client.execute(post);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            result = convertStreamToString(is);

            req.setResult(result);
            req.setSuccess(true);
        }
    } catch (IOException ex) {
        Log.d(TAG, "request failed", ex);
        req.setSuccess(false);
    } catch (URISyntaxException ex) {
        Log.d(TAG, "uri invalid", ex);
        req.setSuccess(false);
    }
}

From source file:com.unifonic.sdk.HttpSender.java

public OTSRestResponse request(String url, List<NameValuePair> data) throws IOException {
    log.debug("URL:" + url);
    log.debug("DATA:" + data);
    return requestDefault(url, new UrlEncodedFormEntity(data, HTTP.UTF_8));
    //return requestDefault(url, new UrlEncodedFormEntity(data));
}

From source file:com.pervasive.sth.network.RESTClient.java

/**
 *
 * @param   payload//from w w w. j av  a 2 s  . c o m
 * @return
 * @throws   Exception
 * @brief   Executes HTTP POST on the server URL with the input payload
 */
public String executePost(String payload) throws Exception {
    if (payload == null) {
        throw new InvalidRESTClientParametersException("Invalid payload ( payload = " + payload + ")");
    }
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(_url);
    httpPost.setHeader(_headerName, _headerValue);
    StringEntity entity = new StringEntity(payload, HTTP.UTF_8);
    httpPost.setEntity(entity);
    HttpResponse response = httpClient.execute(httpPost);
    StatusLine line = response.getStatusLine();
    if (line.getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
        throw new HTTPBadResponseException(line.toString());
    }

    HttpEntity entity1 = response.getEntity();
    return EntityUtils.toString(entity1);
}

From source file:com.lostad.app.base.util.RequestUtil.java

public static String postRequest(final String url, final StringEntity params, String token,
        final boolean isSingleton) throws Exception {
    String json = null;/*from  w  ww .  j a v  a2 s. c  om*/
    HttpClient client = HttpClientManager.getHttpClient(isSingleton);
    HttpEntity resEntity = null;
    HttpPost post = new HttpPost(url);
    if (Validator.isNotEmpty(token)) {
        post.addHeader("token", token);
    }

    try {
        //long t1 = System.currentTimeMillis();

        if (params != null) {
            params.setContentEncoding(HTTP.UTF_8);
            params.setContentType("application/x-www-form-urlencoded");
            post.setEntity(params);
        }

        HttpResponse response = client.execute(post, new BasicHttpContext());
        resEntity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 200
            //?
            json = EntityUtils.toString(resEntity);
        }
    } catch (Exception e) {
        if (post != null) {
            post.abort();
        }
        throw e;
    } finally {

        if (resEntity != null) {
            try {
                resEntity.consumeContent();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        client.getConnectionManager().closeExpiredConnections();
    }
    return json;
}

From source file:no.norrs.projects.andronary.service.utils.HttpUtil.java

public static HttpResponse PUT(String uri, List<NameValuePair> data) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPut httpPost = new HttpPut(uri);
    //httpPost.addHeader("Accept", "application/json");
    httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
    httpPost.addHeader("User-Agent", "Andronary/0.1");
    httpPost.addHeader("Connection", "close");
    StringEntity e = new StringEntity(data.get(0).getValue(), HTTP.UTF_8);
    //httpPost.setEntity(new UrlEncodedFormEntity(data));
    httpPost.setEntity(e);/*from   ww  w.  j a v  a 2 s  .  com*/
    HttpResponse response;

    return httpClient.execute(httpPost);
    //return response.getStatusLine().getStatusCode();
    /*HttpEntity entity = response.getEntity();
    return entity.getContent();*/

}

From source file:hu.balazsbakai.sq.util.RestUtil.java

public void executePOST() throws Exception {
    HttpPost request = new HttpPost(url);

    for (NameValuePair h : headers) {
        request.addHeader(h.getName(), h.getValue());
    }//  w w w  . ja v  a  2 s  .  c o  m

    if (!parameters.isEmpty()) {
        request.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
    }

    executeRequest(request, url);
}