• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help on HttpURLConnection  RSS feed

 
HanMing Low
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have some problem understanding how HttpURLConnection works.
Or, should be why the following code works.
URL url = new URL("http://www.javaranch.com");
conn = (HttpURLConnection)url.openConnection();
As url.openConnection returns a URLConnection and both URLConnection and HttpURLConnection are an abstract classes.
Is there an instance of HttpURLConnection created?
If yes, who implements their abstract methods?
If not, why can it use the instance method?
I refer to the JDK source source
public URLConnection openConnection() throws java.io.IOException {
return handler.openConnection(this);
}
and handler is a URLStreamHandler.
So, I traced to the URLStreamHandler and again it is an abstract method.
abstract protected URLConnection openConnection(URL u) throws IOException;
There should be a factory somewhere, am I right?
I'd greatly appreciate any advice.
Thanks.

Han Ming
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where URLStreamHandlers come from is not a one sentance answer, looking at URL.getURLStreamHandler(String protocol), which is the method called in the URL constructor to locate a URLStreamHandler.
First the method checks if it has already got an instance of the correct URLStreamHandler in a local cache it creates of protocols successfully located previously. If not, it checks if it has a URLStreamHandlerFactory registered and queries the factory for a handler if one does indeed exist. If no factory is registered or the registered factory does not locate a handler, the method uses a handler name guessing method described below. Then it does some synchronized rechecking in case another thread registered a factory or found a URLStreamHandler successfully while it was working earlier. If it has successfully obtained a handler by the end, the handler is added to the cache mentioned earlier and returned, otherwise the method returns null.
Factories are registered with this method:
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
A factory has only one method:
URLStreamHandler createURLStreamHandler(String protocol);
Here are more details on the name guessing algorithm:
The base packages to search should be in the system property "java.protocol.handler.pkgs" as a vertical bar (|) delimited list. The package "sun.net.www.protocol" is searched by default without being in the system property. A base package is turned into a guessed handler by appending "<protocolname>.Handler". For example, the http protocol would cause the method to at least look for class "sun.net.www.protocol.http.Handler". If the system propery contained other base packages, they would be tested too.

I just kinda threw this together looking at the JDK source code. I hope it was useful/interesting.
Dave
[This message has been edited by David Garland (edited December 10, 2001).]
[This message has been edited by David Garland (edited December 10, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!