IntelliJ Java IDE
The moose likes Sockets and Internet Protocols and the fly likes Help on HttpURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Help on HttpURLConnection" Watch "Help on HttpURLConnection" New topic
Author

Help on HttpURLConnection

HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
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

Joined: Jul 27, 2001
Posts: 1365
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).]
 
 
subject: Help on HttpURLConnection
 
Threads others viewed
servlet having trouble with POST data
A question about abstract classes
frame to servlet communication..
java networking. Remote file's last modified date
HttpsURLConnection ClassCastException Question
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture