• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between openConnection() and connect() methods

 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i just want to know the basic difference between openConnection()

and connect() methods of HTTPURLConnection. i have run a program with and

without the connect() method.........and its done............i have also

browsed few sites but didnt recieve the right answer

i thought this is the right place to pose the question

thanks in advance

cheers,
amar
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the Java API Documentation. It appears that you are confusing java.net.URL which has an openConnection method, with java.net.URLConnection and its subclass HttpURLConnection which have a connect method.
 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

i have the following code snippet :



It throws the following exception:

SEVERE: java.io.IOException
Exception in thread "main" java.lang.NullPointerException

In main method im just invoking the above method (called invoke())

suggestions are most welcome and awaited

cheers,
amar
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a Bad Idea to encode a valid URL.

One only needs to encode parts of a URL which may contain control characters (i.e space, '/', ':') OTHER than where those control characters are actually needed, like in the base URL.
 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

as i have already mentioned that it was a sample code and not the exact URL.

im working on a project in the Singapore government account and the details

of the corresponding URL is highly confidential( atleast at this point of

time ).

Any have im trying to use the proxy settings for this URL.

Solutions and suggestions are welcome.

cheers,
amar
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amarender reddy:

as i have already mentioned that it was a sample code and not the exact URL.

It doesn't matter what the URL is, encoding it is a bad idea.

Originally posted by amarender reddy:
Any have im trying to use the proxy settings for this URL.


Java Networking and Proxies
 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Hey Reddy this is usually what i would do.



I don't know why encoded your URL because that's not how the url works.
The purpose of the URLEncoder is to encode strings that are sent as messages over the wire to the server or vice-versa and has nothing to do with the URL itself.

For the main question itself the HttpURLConnection has connect because the main contructor is protected(same as for URLConnection)so it has connect(if you want to subclass that class itself) but everything else works the same.

so basically you can even do this if your URL has escape characters




I quote this from sun's documentation

"Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL().

The URLEncoder and URLDecoder classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396."

This is the URI class documentation

http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html

Sorry i don't mean to hijack your thread but i have a question for joe ess

Joe you said this

"One only needs to encode parts of a URL which may contain control characters (i.e space, '/', ':') OTHER than where those control characters are actually needed, like in the base URL."

Now from what you are saying, i can't do this as it won't work



but i must do this for it to work



I hope that you could please clarify this Joe Ess

Richard West
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by West Richard:

Now from what you are saying, i can't do this as it won't work



I don't see what part of my statement says that your code example will not work. I was thinking about something like this example from Java Almanac, where one encodes only the searchpart of a request.
 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Joe ess, You said this

I don't see what part of my statement says that your code example will not work. I was thinking about something like this example from Java Almanac, where one encodes only the searchpart of a request.

Now in that example there was no encoding done on the URL itself but only to the data that is a string to be sent over to the other side as part of a query.

Consider this joe with this URL.

http://www.javaranch.com

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

This way



Or this way



Are both correct or both wrong or nly 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.

I hope that you could please clarify this Joe Ess

Richard West
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by West Richard:

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


Both will work. The first example will work because you don't have any unusual control characters in the searchpart of the request. Suppose that it had a search request where you had a few terms seperated by spaces and it would not work. For more about URL's, have a gander at the specification:
RFC 1738 - Uniform Resource Locators, specifically section 2.2. URL Character Encoding Issues.
The "correct" way to connect to a URL is dictated by the requirements of a particular situation. That's why there's More Than One Way.


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.


java.net.URI can be used in that capacity, but URL's and URI's have distinct properties:
A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. . .

From the link to the java.net.URI javadoc you cited earlier.
 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

From what i read in the rfc it would 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??



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.

I hope that you could please clarify this Joe Ess

Richard West
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you ment something like this:

Because URL doesn't have a toASCIIString method, and even if it did, uri.toURL() would throw a MalformedURLException when it found non-ASCII characters.
I've never found it necessary to take into account Unicode characters which are not in the US-ASCII character set so I'm kind of out of my depth here, but if you had a requirement to handle that case, I'd say that code would be fairly safe.
Personally, I've gotten great mileage from code very similar to the example from the Java Almanac I pointed to above.
 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Sorry about that wrong code. What i meant was something like that



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 you could please clarify this Joe Ess

Richard West
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!