Are there any performance advantages to be gained by using a Socket to connect to an HTTP server, instead of URL or URLConnection?

Tim Rohaly

Using a Socket gives you more control over the connection - you can set socket options, use your own streams, or even use your own socket implementation factory. However, this comes at great expense; URL and URLConnection do a lot of things for you that you would need to implement from scratch. For example, you would have to completely implement the HTTP protocol, including support for proxies, maintain this code, and keep it up to date as HTTP evolves. URL and URLConnection let you extend functionality through content and protocol handlers - you would need to implement this mechanism yourself or lose that extensibility. I don't know why you would want to do this - the network overhead for HTTP dwarfs any potential savings in CPU time you might get from dealing with low-level sockets, and you lose generality, flexibility, and maintainability.

If you're convinced you really want control at the socket level, I would suggest that you consider writing and using your own protocol handler instead of reinventing URL and URLConnection.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact