Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpURLConnection and URL with special characters

Consider this following code (that retrieves the response from a HTTP request and prints it). NOTE: This code works in a standard Java application. I only experience the problem listed below when using the code in an Android application.

public class RetrieveHTMLTest {

public static void main(String [] args) {
    getListing(args[0);
}

public static void getListing(String stringURL) {

    HttpURLConnection conn = null;
    String html = "";
    String line = null;
    BufferedReader reader = null;
    URL url = null;

    try {
        url = new URL(stringURL);

        conn = (HttpURLConnection) url.openConnection();

        conn.setConnectTimeout(6000);
        conn.setReadTimeout(6000);
        conn.setRequestMethod("GET");

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        conn.connect();

        while ((line = reader.readLine()) != null) {
            html = html + line;
        }

        System.out.println(html);

        reader.close();
        conn.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {

    }
}   
}

If I supply the URL: http://somehost/somepath/

The following code works fine. But, if I change the URL to: http://somehost/somepath [a comment]/ The code throws a timeout exception because of the "[" and "]" characters.

If I change the URL to: http://somehost/somepath%20%5Ba%20comment%5D/ The code works fine. Again, because the "[" and "]" characters aren't present.

My question is, how do I get the URL:

http://somehost/somepath [a comment]/

into the following format:

http://somehost/somepath%20%5Ba%20comment%5D/

Also, should I continue using HttpURLConnection in Android since it can't accept a URL with special characters? If the standard to always convert the URL before using HttpURLConnection?

like image 542
William Seemann Avatar asked Dec 06 '11 04:12

William Seemann


People also ask

Can URL contains special characters?

Building a valid URL A URL entered within an address bar in a browser, for example, may contain special characters (e.g. "上海+中國" ); the browser needs to internally translate those characters into a different encoding before transmission.

Which characters need URL encoding?

It is sometimes called URL encoding. The encoding consists of substitution: A '%' followed by the hexadecimal representation of the ASCII value of the replace character. Special characters needing encoding are: ':' , '/' , '?' , '#' , '[' , ']' , '@' , '!'

What is HttpURLConnection in Android?

HttpsURLConnection. HttpsURLConnection. HttpsURLConnection extends HttpURLConnection with support for https-specific features. The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL.

How do I set cookies in HttpURLConnection?

Setting a cookie value in a request:URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp"); URLConnection urlConn = myUrl. openConnection();


1 Answers

Use the URLEncoder class :

URLEncoder.encode(value, "utf-8");

You can find more details here.

Edit : You should use this method only to encode your parameter values. DO NOT encode the entire URL. For example if you have a url like : http://www.somesite.com?param1=value1&param2=value2 then you should only encode value1 and value2 and then form the url using encoded versions of these values.

like image 148
Arnab Chakraborty Avatar answered Oct 29 '22 19:10

Arnab Chakraborty