Marco van Hilst
Marco van Hilst

Reputation: 619

Route an Http(s)URLConnection through a pre-existing Socket

I've spent a few infuriating hours poring over the javax.net and sun.net SSL implementations and the Apache HttpClient 4.3 SSL handling (which uses the javax.net facilities at its core by default) and have made frustratingly little progress. I'm attempting to tunnel traffic from an HttpsURLConnection through previously created Input/OutputStreams in order to have access to the HTTP java.net utilities while being able to send the data through my own streams. I originally figured I could hackishly route the HttpsURLConnection through a custom Socket instance with a SocketImpl that uses these Input/OutputStreams, but it appears that I would have to change the default HttpsURLConnection SSLFactory in order to achieve that, which is not viable as the application is multi-threaded. I'm open to alternate libraries and hackish fixes!

The end goal I wish to achieve: Http(s)URLRequest (or equivalent HTTP facility that supports SSL) -> My Custom Socket -> Internet

Upvotes: 2

Views: 789

Answers (1)

user207421
user207421

Reputation: 310840

It seems to me that your best option is to install your own SSLSocketFactory on each relevant HttpsURLConnection instance. It can create an SSLSocket instance that delegates to a real one but returns your streams wrapped around the delegate's streams.

If you need access to the cipher text for some reason, you could turn that upside down and supply your own Socket to SSLSocketFactory.createSocket(Socket, ...),

Upvotes: 2

Related Questions