Hi everyone,

Consider this URL.

"http://forums.devx.com/newthread.php?do=newthread&f=104"

Some URLs have something that are called escape characters("?", etc")

Are these characters supposed to encoded using the URI class that has a method called toASCIIString() that returns the escaped characters as a US ASCII format

or simply by direct connection

or create a URI to URL conversion by using its toURL method

Which is actually the correct way of connecting to that URL??

This way

HttpURLConnection connection = null; 
String p1 = "http://forums.devx.com/newthread.php?do=newthread&f=104";

URL url = new URL(p1);
connection = (HttpURLConnection)url.openConnection();    connection.connect();  

//other codes to open the stream and read from it

Or this way

HttpURLConnection connection = null; 
String p1 = "http://forums.devx.com/newthread.php?do=newthread&f=104";

URI uri = new URI(p1);
URL url = uri.toURL();

connection = (HttpURLConnection)url.openConnection();    connection.connect(); 

 //other codes to open the stream and read from it

or this way

HttpURLConnection connection = null; 
String p1 = "http://forums.devx.com/newthread.php?do=newthread&f=104";

URI uri = new URI(p1);
URL url = new URL(uri.totoASCIIString());

connection = (HttpURLConnection)url.openConnection();    connection.connect(); 

 //other codes to open the stream and read from it

Are both correct or both wrong or only one is correct??

One more thing but isn't the purpose of the Java URI class supposed to encode any of the query statements in the URL and pass it back to the URL class using its toURL method. Correct me if i am wrong.

If i am wrong then what's the purpose of the Java URI class

I hope that someone could please clarify this

Thank You

Yours Sincerely

Richard West

Hi everyone,

I read my first post and i don't think i did it correctly so here i go again so please bear with me for a while

I am trying to connect to a URL but the thing is that this URL (whether it exist or not) has some spaces in it and also has some illegal characters in it(which must be encoded using US-ASCII according to the RFC). Basically the below code is something i wrote that i think that should be able to take care of most of these situations

Is the below code a good way of making sure that the URL if it has spaces or has illegal characters that needs to be encoded??

HttpURLConnection connection = null; 

String p1 = "http://www.codeguru.com/forum/newthread .php?do=newt\hread&f=5";

URI uri1 = new URI(p1); 

//This below line takes care of spaces of any other funny stuff

String p2 = uri.toURL().toString(); 

URI uri2 = new URI(p2); 

//The below line encodes any illegal characters or as required if it does 
//not conform to the RFC 

String p3 = uri2.toASCIIString();  

URL url1 = new URL(p3); 

connection = (HttpURLConnection)url.openConnection();   connection.connect();  

 //other codes to open the stream and read from it

Wouldn't the above way be a better way of writing a URL code in which if there were spaces(fills it in with an appropriate value) or illegal values(its encoded to US-ASCII) practically work for most situations.

I hope that someone could please clarify this for me

Richard West

Hi everyone,

Take a look at URLEncoder. It will encode the URL String into a String you can pass to a URL object.

The URLEncoder class is for the encoding of strings that is to be passed over the wire(between a client and server). I think sun needs to change the name of that class.

For example if i used the URL Encoder this is what i will get

www.google.com/icons.jpg

becomes

www.google.com%2Ficons.jpg

Why does the '/' get converted even when obviously its not an illegal character.

That address may exist but if pass that to the URL Object it will throw exception and i don't blame if it does

This is where the URI class comes to play.

My question is basically is whether i am using the URI class so that if thatere are spaces its taken care of and if there are illegal characters its encoded(not the entire url). It is very important for me to adhere to the URL RFC or else nothing will work .

Richard West

This article has been dead for over six months. Start a new discussion instead.