#2446 How to use WebClient to make a HTTP request from behind a proxy that requires authentication?

LightDye Wed 26 Aug 2015

Hi,

I'm trying to make a HTTP request from a computer that sits behind a proxy and the proxy requires authentication.

If I do something like this:

using web
class Main
{
  Void main()
  {
    wc := WebClient()
    wc.proxy = `http://myproxy:80`
    wc.reqUri = `http://www.google.com`
    s := wc.writeReq.readRes.resIn.readLine
    echo(s)
  }
}

I get this output:

<HEAD><TITLE>Proxy Authorization Required</TITLE></HEAD>

I have tried using:

wc.proxy = `http://myUsername:myPassword@myproxy:80`

And also setting the system environment variable HTTP_PROXY including the username and password and even this:

using [java] java.lang::System
using web
class Main
{
  static {
    System.setProperty("http.proxyUser","myUsername");
    System.setProperty("http.proxyPassword","myPassword");
  }
  ...
}

However, I cannot get this to work. Is proxy authentication supported in Fantom?

If not, can I use a Java HttpClient that supports proxy authentication? Does anyone has an example, please?

SlimerDude Thu 27 Aug 2015

Looking at the src, there's no code in WebClient for handling proxy authentication, so you'll have to handle it yourself.

For basic authentication, and looking at RFC2608 sec. 14.34, it should be as simple as:

wc := WebClient()
wc.proxy = `http://myproxy:80`
wc.reqHeaders["Proxy-Authorization"] = "Basic " + "${user}:${pass}".toBuf.toBase64

LightDye Thu 27 Aug 2015

Thanks @SlimerDude, it makes sense. I'll try this as soon as I'm back behind the proxy.

brian Thu 27 Aug 2015

Also remember that Fantom's proxy settings are configured in etc/web/config.props

LightDye Fri 28 Aug 2015

Thanks again @SlimerDude. It seems that this line...

wc.reqHeaders["Proxy-Authorization"] = "Basic " + "${user}:${pass}".toBuf.toBase64

...is doing the proxy authentication successfully since it is no longer replying with a HTTP 407 error. I get a 200 now. However my request is still not going through the proxy.

wc.followRedirects = true // doesn't help.

I will have to investigate this further.

@Brian Thanks for pointing out the right location of the proxy settings, but if WebClient doesn't implement proxy authentication I don't think there will be any properties there to specify the user credentials, would they?

SlimerDude Fri 28 Aug 2015

Here are the settings from etc/web/config.props

// Default WebClient.proxy URI formatted as "http://{host}[:port]/"
// proxy=http://foo:8080/

// WebClient.proxy exceptions as comma separated list of Regex globs
// proxy.exceptions=192.168.*.*,*.google.com

They prevent you from having to manually specify WebClient.proxy for each request. No authentication logic in there just yet.

Login or Signup to reply.