Example usage for java.net URLEncoder encode

List of usage examples for java.net URLEncoder encode

Introduction

In this page you can find the example usage for java.net URLEncoder encode.

Prototype

public static String encode(String s, Charset charset) 

Source Link

Document

Translates a string into application/x-www-form-urlencoded format using a specific java.nio.charset.Charset Charset .

Usage

From source file:core.RESTCalls.RESTPost.java

public static String httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception {

    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");

    conn.setDoOutput(true);/*from   ww  w . j ava2 s . c om*/
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    OutputStream out = conn.getOutputStream();

    Writer writer = new OutputStreamWriter(out, "UTF-8");

    for (int i = 0; i < paramName.length; i++) {

        writer.write(paramName[i]);
        writer.write("=");
        writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
        writer.write("&");
    }

    writer.close();
    out.close();

    if (conn.getResponseCode() != 200)
        throw new IOException(conn.getResponseMessage());

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    StringBuilder sb = new StringBuilder();

    String line;

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

    rd.close();

    conn.disconnect();

    return sb.toString();
}

From source file:com.xidu.framework.common.util.EnDecoderUtils.java

/**
 * encode input string by specific coding.
 * /*  ww  w  .  j  a v  a2  s.  co  m*/
 * @Date : 2011-3-23
 * @param input
 *            - string
 * @param coding
 *            - the coding name
 * @return encoded string
 */
public static String encode(String input, String coding) {
    if (StringUtils.isBlank(input)) {
        return "";
    }
    try {
        return URLEncoder.encode(input, coding);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return input;
}

From source file:com.gisgraphy.rest.BeanToRestParameter.java

public static String toQueryString(Object object) {
    if (object == null) {
        throw new RestClientException("Can not get queryString for null object");
    }//from   w w  w. j  a  va 2  s  .  c om
    StringBuilder sb = new StringBuilder(128);
    try {
        boolean first = true;
        String andValue = "&";
        for (PropertyDescriptor thisPropertyDescriptor : Introspector
                .getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) {
            Object property = PropertyUtils.getProperty(object, thisPropertyDescriptor.getName());
            if (property != null) {
                sb.append(first ? "?" : andValue);
                sb.append(thisPropertyDescriptor.getName());
                sb.append("=");
                sb.append(URLEncoder.encode(property.toString(), Constants.CHARSET));
                first = false;
            }
        }
    } catch (Exception e) {
        throw new RestClientException("can not generate url for bean: " + e.getMessage(), e);
    }
    return sb.toString();
}

From source file:at.tugraz.sss.serv.datatype.SSUri.java

public static boolean isURI(final String string) {

    //import org.apache.commons.httpclient.URIException;
    //import org.apache.commons.httpclient.util.URIUtil;
    //    URIUtil.encodeQuery(string)
    //    new URL(uriString); //import java.net.URL;
    if (string == null) {
        return false;
    }//from ww w .  j a  v a 2  s.co  m

    try {

        final URL url = new URL(string);

        java.net.URI.create(string);

        URLEncoder.encode(string, SSEncodingU.utf8.toString());

        if (string.length() > 250) {
            SSLogU.warn("uri too long (> 250 chars) to be stored in sss", null);
            return false;
        }

        return true;

    } catch (MalformedURLException | UnsupportedEncodingException error) {
        SSLogU.trace(error, false, false);
        return false;
    }
}

From source file:com.ibm.streamsx.topology.internal.streaminganalytics.RestUtils.java

public static String getJobSubmitURL(JsonObject credentials, File bundle) throws UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder(500);
    sb.append(jstring(credentials, "rest_url"));
    sb.append(jstring(credentials, "jobs_path"));
    sb.append("?");
    sb.append("bundle_id=");
    sb.append(URLEncoder.encode(bundle.getName(), StandardCharsets.UTF_8.name()));
    return sb.toString();
}

From source file:com.moss.bdbadmin.api.util.KeyFactory.java

public static String encode(byte[] key) {
    try {/*  w  ww.j  a va 2  s  .c om*/
        byte[] base64 = Base64.encodeBase64(key);
        String utfBase64 = new String(base64, ENCODING);
        String urlEncoded = URLEncoder.encode(utfBase64, ENCODING);
        return urlEncoded;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.nttdata.depend.util.OAuthUtil.java

public static String getLoginURL() throws UnsupportedEncodingException {
    StringBuilder loginURL = new StringBuilder(OAUTH_LOGIN_URL);
    loginURL.append("?");
    loginURL.append("response_type=code");
    loginURL.append("&");
    loginURL.append("client_id=").append(clientId);
    loginURL.append("&");
    loginURL.append("redirect_uri=").append(URLEncoder.encode(requestURI, "UTF-8"));
    return loginURL.toString();
}

From source file:vazkii.psi.client.core.helper.SharingHelper.java

public static void uploadAndShare(String title, String export) {
    String url = uploadImage(title, export);

    try {//  w  ww.  j a va  2 s.  com
        String contents = "## " + title + "  \n" + "### [Image + Code](" + url + ")\n"
                + "(to get the code click the link, RES won't show it)\n" + "\n" + "---" + "\n"
                + "*REPLACE THIS WITH A DESCRIPTION OF YOUR SPELL  \n"
                + "Make sure you read the rules before posting. Look on the sidebar: https://www.reddit.com/r/psispellcompendium/  \n"
                + "Delete this part before you submit.*";

        String encodedContents = URLEncoder.encode(contents, "UTF-8");
        String encodedTitle = URLEncoder.encode(title, "UTF-8");

        String redditUrl = "https://www.reddit.com/r/psispellcompendium/submit?title=" + encodedTitle + "&text="
                + encodedContents;

        if (Desktop.isDesktopSupported())
            Desktop.getDesktop().browse(new URI(redditUrl));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:io.knotx.adapter.common.post.UrlEncodedBodyBuilder.java

private static String encodeComponent(String s, Charset charset) {
    try {/*from w ww . j a v  a  2s  .com*/
        return URLEncoder.encode(s, charset.name()).replace("+", "%20");
    } catch (UnsupportedEncodingException ignored) {
        throw new UnsupportedCharsetException(charset.name());
    }
}

From source file:ShoppingCartViewerRewrite.java

private static String generateSessionId() throws UnsupportedEncodingException {
    String uid = new java.rmi.server.UID().toString(); // guaranteed unique
    return URLEncoder.encode(uid, "UTF-8"); // encode any special chars
}