Scala Proxy Authentication

Do you like this?

Summary:
This short post shows you how to do authentication for proxy in Scala.


Content:

Disclaimer: I have been using this technique in Java for a while and forgot where I actually copied the code from. Declare a ProxyAuthenticator class as follows.


import java.net.Authenticator
import java.net.PasswordAuthentication

class ProxyAuthenticator(user: String, password: String) extends Authenticator {

  def this() = this("default_user_name", "default_password")

  override def getPasswordAuthentication(): PasswordAuthentication = {
    return new PasswordAuthentication(user, password.toCharArray());
  }
}
Now you can use it wherever you want.
import java.net.URL
import java.net.Authenticator

object Test extends App {
  Authenticator.setDefault(new ProxyAuthenticator());
  System.getProperties().put("http.proxyHost", "your-host");
  System.getProperties().put("http.proxyPort", "port-number");
  val url = new URL("http://your-website.com")
  
}

 
comments powered by Disqus