URI

F

freesoft_2000

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
 
C

Chris Smith

freesoft_2000 said:
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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
F

freesoft_2000

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
 
F

freesoft_2000

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top