简体   繁体   中英

How do I use proxy authentication with an SSL connection using HttpsUrlConnection?

I am trying to connect to a web site that uses SSL. This connection is via proxy (squid) that also requires proxy authentication.

I am currently using HttpsUrlConnection for connections within my app and would prefer to continue to use this if it is possible.

The problem occurs when you make the connection using connection.open(" https://domain.com "), the HTTP CONNECT is sent immediately before you have had a chance to set any headers including "Proxy-Authentication", in a normal http connection nothing is sent until getOutputStream is called, so just setting this header works.

It is possible to set a defaultAuthenticator for this that returns the correct name and password. There are two problems with this though:

  1. The application is a web application running inside a web server with unknown other applications. If I set a default authenticator, would this effect other applications? (for the same reason setting system properties is out)

  2. I also need the ability to use different proxies in different connections. eg I may have a connection that goes to proxy1.domain.com and another that goes to proxy2.domain.com, each with different credentials.

What is the correct way of achieving this? I would be very surprised if this was not possible for HttpUrlConnection given its widespread use.

If I'm not totally mistaken this cannot be achieved using the JVM URLConnection and friends. Besides, given its track record in terms of severe bugs and memory leaks it has been on our corporate black list for quite some time.

Use Apache HttpClient .

As for your question 1, in general each ClassLoader has its own copy of a class and the classes static members. Since, Authenticator is a JVM class I assume it's loaded by (one of) the parent ClassLoader s of your application ClassLoader which assures there's only a single copy of the Authenticator class inside the JVM.

您设置一个java.net.Authenticator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM