> Which is actually the correct way of connecting to that URL??
The second of your methods is probably best. Because of bugs (that Sun
has intentionally chosen to retain for backward compatibility... against
all semblance of reason), you should never use new URL(String). It
fails to do encoding correctly in some cases. Creating a new URI and
then calling toURL works fine. Your third option, using toASCIIString,
would work but is not necessary.
> 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
URI is basically intended to be a fixed version of URL, that doesn't
have the same link to actual protocol implementations. It's essentially
a utility class for doing URI-specific kinds of text manipulation. You
can use it to get a URL object and use that, or you could just use it to
build a well-formed URI, call toString(), and write that to a config
file or something of the sort.
I don't see any specific code in java.net.URI to support building an
HTTP query string, though. All you can do is pass in a pre-existing
string.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
freesoft_2000 - 27 Jul 2005 21:15 GMT
Hi everyone,
Chris from what i read in the rfc it would it not seem better if i wrote
my code in this way.
Is this a correct way of getting the URL to work with most situations
including illegal characters where encoding is neccesary and would it
work??
HttpURLConnection connection = null;
String p1 =
"http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=reply&f=8&t=002161&java";
URI uri = new URI(p1);
String p2 = uri.toURL().toASCIIString();
URL url = new URL(p2);
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 writting 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.
Hoping to hear from you
Richard West
freesoft_2000 - 28 Jul 2005 15:52 GMT
Hi everyone,
I read my first post and i don't think i explained 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