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:MainClass.java

public static void main(String[] args) throws Exception {
    System.out.println(URLEncoder.encode("A=B=C=D=E", "UTF-8"));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    System.out.println(URLEncoder.encode("A/B/C/", "UTF-8"));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String line = URLEncoder.encode("name1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");

    String[] pairs = line.split("\\&");
    for (int i = 0; i < pairs.length; i++) {
        String[] fields = pairs[i].split("=");
        String name = URLDecoder.decode(fields[0], "UTF-8");
        System.out.println(name);
        String value = URLDecoder.decode(fields[1], "UTF-8");
        System.out.println(value);
    }/*from  w w w  .  j  a  v a  2  s  . c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = "index&^%*a test for 2.5% and  &";
    String encoded = URLEncoder.encode(source, "utf-8");
    String decoded = URLDecoder.decode(encoded, "utf-8");
    System.out.println("Source: " + source);
    System.out.println("Encoded: " + encoded);
    System.out.println("Decoded: " + decoded);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

    URL url = new URL("http://server.com:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);/* w  ww . j ava2 s .  co m*/
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");

    Socket socket = new Socket("127.0.0.1", 8080);

    String path = "/servlet";
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");

    wr.write(data);//from  w ww .j av a  2 s  . com
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
}

From source file:com.serena.rlc.provider.jenkins.JenkinsTest.java

/**
 * @param args the command line arguments
 *//*w w  w  .  j a v  a2 s. c  o  m*/
public static void main(String[] args) throws ProviderException, IOException {
    System.out.println(URLEncoder.encode(
            "http://10.31.25.44:8080/jenkins/job/Build Job 2/api/json?tree=actions[parameterDefinitions[*,defaultParameterValue[*]]]&pretty=1",
            "UTF-8"));
    /* ApplicationContext context = new ClassPathXmlApplicationContext("serena-provider-jenkins-1.0.xml");
            
     JenkinsExecutionProvider jenkinsProvider = (JenkinsExecutionProvider) context.getBean("executionProvider");
            
     System.out.println(jenkinsProvider.toString());
             
     ActionInfoResult actions = jenkinsProvider.getActions();
             
     ActionInfo ai = jenkinsProvider.getActionInfo("buildJob");*/
    /*
            FieldInfo fieldInfo = jenkinsProvider.getFieldValues("objecttype", null);
            
            for (FieldValueInfo val : fieldInfo.getValues()) {
    System.out.println(val.getId() + " - " + val.getName());
            }
            
            List<Field> properties = new ArrayList<>();
            
            properties.add(new Field("objecttype", "objecttype", "defect"));
            
            properties.add(new Field("project", "project", ""));
            properties.add(new Field("workspace", "workspace", "/workspace/12345"));
            properties.add(new Field("fetch", "fetch", ""));
            properties.add(new Field("query", "query", ""));
            properties.add(new Field("order", "order", ""));
            properties.add(new Field("scopeup", "scopeup", ""));
            properties.add(new Field("scopedown", "scopedown", ""));
    */
    //ExecutionInfo results = jenkinsProvider.execute("Build", null, null, null);

}

From source file:org.openiot.gsn.http.rest.RemoteWrapperHttpClient.java

public static void main(String[] args) throws ClientProtocolException, IOException, ClassNotFoundException {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    final String url = "select * from localsystemtime where timed > 10/10";

    HttpGet httpget = new HttpGet(
            "http://localhost:22001/streaming/" + URLEncoder.encode(url, "UTF-8") + "/12345");
    HttpResponse response = httpclient.execute(httpget);

    ObjectInputStream out = (StreamElement4Rest.getXstream())
            .createObjectInputStream((response.getEntity().getContent()));
    DataField[] structure = (DataField[]) out.readObject();
    StreamElement4Rest se = null;/*from w  w w  .j  a  v a2 s  . c o m*/
    try {
        while ((se = (StreamElement4Rest) out.readObject()) != null) {
            System.out.println(se.toStreamElement());
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

From source file:org.salever.rcp.remoteSystem.server.util.ClientAbortMethod.java

public final static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {//from  w  ww .  ja v a  2s  . com

        String queryString = "wd=?ddd=";
        String decodeUrl = URLEncoder.encode(queryString, "GBK");
        String string = new String(decodeUrl);
        System.out.println("--------------" + string + "--------------------------");
        HttpGet httpget = new HttpGet("http://www.baidu.com/s?" + string);

        System.out.println("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
            String line;
            while ((line = reader.readLine()) != null) {
                // System.out.println(line);
            }
        }
        System.out.println("----------------------------------------");

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.craftercms.cstudio.publishing.StopServiceMain.java

public static void main(String[] args) throws Exception {
    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
            "classpath:spring/shutdown-context.xml");

    ReadablePropertyPlaceholderConfigurer properties = (ReadablePropertyPlaceholderConfigurer) context
            .getBean("cstudioShutdownProperties");
    if (properties != null) {
        String url = getProperty(properties, PROP_URL);
        String path = getProperty(properties, PROP_SERVICE_PATH);
        String port = getProperty(properties, PROP_PORT);
        String password = URLEncoder.encode(getProperty(properties, PROP_PASSWORD), "UTF-8");
        String target = url + ":" + port + path;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Sending a stop request to " + target);
        }/*from w w  w . j a  v a  2  s . co  m*/
        target = target + "?" + StopServiceServlet.PARAM_PASSWORD + "=" + password;
        URL serviceUrl = new URL(target);
        HttpURLConnection connection = (HttpURLConnection) serviceUrl.openConnection();
        connection.setRequestMethod("GET");
        connection.setReadTimeout(10000);
        connection.connect();
        try {
            connection.getContent();
        } catch (ConnectException e) {
            // ignore this error (server will terminate as soon as the request is sent out)
        }
    } else {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error(PROPERTIES_NAME + " is not present in shutdown-context.xml");
        }
    }
    context.close();
}