Example usage for org.apache.http.client.utils URLEncodedUtils format

List of usage examples for org.apache.http.client.utils URLEncodedUtils format

Introduction

In this page you can find the example usage for org.apache.http.client.utils URLEncodedUtils format.

Prototype

public static String format(final Iterable<? extends NameValuePair> parameters, final Charset charset) 

Source Link

Document

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Usage

From source file:org.github.oauth2.OAuth2Utils.java

/**
 * Generate authorize url for given client with given scope
 * /*from w  w  w .  j  a v  a2  s.c om*/
 * @param client
 * @param scope
 * @return authorize url
 */
public static String getAuthorizeUrl(OAuth2Client client, String scope) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(IOAuth2Constants.PARAM_REDIRECT_URI, client.getRedirectUri()));
    params.add(new BasicNameValuePair(IOAuth2Constants.PARAM_CLIENT_ID, client.getId()));
    if (scope != null && scope.length() > 0)
        params.add(new BasicNameValuePair(IOAuth2Constants.PARAM_SCOPE, scope));
    String query = URLEncodedUtils.format(params, null);
    return client.getAuthorizeUrl() + '?' + query;
}

From source file:server.web.HttpRequestHelper.java

public static String sendingRequest(String url, String data) throws IOException {

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair(HttpMetods.DATA, data));

    String sendingUrl = "http://" + url;

    log.info("Sending 'GET' request to URL : " + sendingUrl);
    log.info("GET parameters : " + URLEncodedUtils.format(params, "utf-8"));
    log.info("Request : " + sendingUrl + "?" + URLEncodedUtils.format(params, "utf-8"));

    HttpGet request = new HttpGet(sendingUrl + "?" + URLEncodedUtils.format(params, "utf-8"));
    request.setHeader("charset", "UTF-8");
    request.setHeader("Content-Type", "application/json");

    return send(request);
}

From source file:og.android.tether.system.WebserviceTask.java

public static HttpResponse makeRequest(String url, List<BasicNameValuePair> params)
        throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    String paramString = URLEncodedUtils.format(params, "utf-8");
    Log.d(MSG_TAG, url + "?" + paramString);
    HttpGet request = new HttpGet(url + "?" + paramString);
    return client.execute(request);
}

From source file:org.apache.bigtop.bigpetstore.qstream.Utils.java

public static HttpResponse get(String hostname, String resource, List<? extends NameValuePair> params)
        throws Exception {
    System.out.println("getting http " + hostname);
    //URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
    URI uri = URIUtils.createURI("http", hostname, -1, resource, URLEncodedUtils.format(params, "UTF-8"), null);
    System.out.println(uri.toASCIIString());
    HttpResponse respo = get(uri);//from w  w w.j a  v a  2 s. c  om
    return respo;
}

From source file:com.foobnix.util.LyricUtil.java

public static String fetchLyric(String artist, String title) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("artist", artist));
    params.add(new BasicNameValuePair("song", title));
    params.add(new BasicNameValuePair("fmt", "json"));

    String paramsList = URLEncodedUtils.format(params, "UTF-8");
    HttpGet request = new HttpGet("http://lyricwiki.org/api.php?" + paramsList);
    HttpClient client = new DefaultHttpClient();

    try {//  w w w. ja  v a  2s .co m
        HttpResponse execute = client.execute(request);
        String response = EntityUtils.toString(execute.getEntity());

        LOG.d(response);

        if (response != null && response.indexOf('{') > 0) {
            JSONObject obj = new JSONObject(response.substring(response.indexOf('{')));
            return obj.getString("lyrics");
        }
    } catch (ClientProtocolException e) {
        LOG.e("", e);
    } catch (IOException e) {
        LOG.e("", e);
    } catch (JSONException e) {
        LOG.e("", e);
    }

    return "Lyric not found";
}

From source file:com.netdimensions.client.Command.java

final String url() {
    return action + "?" + URLEncodedUtils.format(parameters, "UTF-8");
}

From source file:com.hoiio.sdk.util.StringUtil.java

/**
 * Converts the {@code Map} to URL encoded {@code String}
 * @param map {@code Map<String, String>}
 * @return The encoded URL {@code String}
 *//*from   ww  w.  j  a v a  2s.  c  o m*/
public static String convertMapToUrlEncodedString(Map<String, String> map) {
    if (map == null) {
        return "";
    }
    List<NameValuePair> qparams = new LinkedList<NameValuePair>();
    for (Entry<String, String> item : map.entrySet()) {
        if (item.getValue() != null)
            qparams.add(new BasicNameValuePair(item.getKey(), item.getValue()));
    }
    return URLEncodedUtils.format(qparams, "UTF-8");
}

From source file:edu.illinois.whereru.HTTPRequest.java

public static JSONObject makeHttpRequest(String urlString, String method, List<NameValuePair> params) {

    JSONObject jsonObject = null;//from w w w.ja va  2  s. c  o m

    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse;

        if (method.equals("POST")) {
            // init post
            HttpPost httpPost = new HttpPost(urlString);
            // set the resource to send
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            // send the request, retrieve response
            httpResponse = httpClient.execute(httpPost);

        } else {
            // GET method
            // formulate url
            String paramString = URLEncodedUtils.format(params, "utf-8");
            urlString += "?" + paramString;
            // init GET
            HttpGet httpGet = new HttpGet(urlString);
            // send the request, retrieve response
            httpResponse = httpClient.execute(httpGet);

        }

        // retrieve content from the response
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream is = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        jsonObject = new JSONObject(sb.toString());
    } catch (NullPointerException e) {

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}

From source file:ch.iterate.openstack.swift.model.Region.java

public URI getStorageUrl(List<NameValuePair> parameters) {
    return URI
            .create(String.format("%s?%s", this.getStorageUrl(), URLEncodedUtils.format(parameters, "UTF-8")));
}

From source file:com.hubrick.vertx.s3.util.UrlEncodingUtils.java

public static String addParamsSortedToUrl(String url, Map<String, String> params) {
    checkNotNull(url, "url must not be null");
    checkNotNull(!url.isEmpty(), "url must not be empty");
    checkNotNull(params, "params must not be null");
    checkNotNull(!params.isEmpty(), "params must not be empty");

    final String baseUrl = extractBaseUrl(url);
    final String urlParams = baseUrl.equals(url) ? "" : url.replace(baseUrl + "?", "");
    final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(urlParams, Charsets.UTF_8);

    for (Map.Entry<String, String> paramToUrlEncode : params.entrySet().stream()
            .sorted(Comparator.comparing(Map.Entry::getKey)).collect(Collectors.toList())) {
        nameValuePairs.add(new BasicNameValuePair(paramToUrlEncode.getKey(), paramToUrlEncode.getValue()));
    }/* w ww  . j a  v a  2 s.  c om*/

    return baseUrl + "?" + URLEncodedUtils.format(nameValuePairs, Charsets.UTF_8);
}