wood burning stoves*
The moose likes Sockets and Internet Protocols and the fly likes accessing URL through java from behind a fire wall: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "accessing URL through java from behind a fire wall: " Watch "accessing URL through java from behind a fire wall: " New topic

accessing URL through java from behind a fire wall:

Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Hi All,

I am a newbie to java.net packages and am facing a problem

Let me explain my environment before describing my problem

I have to access a URL from behind my fire wall

I am able to access the URL from my Web browser. So I assume I am authorized to do so

I know my proxy settings

This is the code I use

The above code throws an exception
java.io.IOException: Server returned HTTP response code: 407 for URL: http://www.javaranch.com/

First question response code 407 means authorisation needed then why am I able to access the URL through browser without any authentication, so I guess it is something to do with code.

Now I modify the code a bit and add

Where PasswordAuthenticator is an subclass of Authenticator overridding its protected PasswordAuthentication getPasswordAuthentication()method returning the user-id and password I use to log on to windows

Now I get a different error

java.net.ProtocolException: Server redirected too many times (20)

I really can't think of anything else to access this URL through java. Any pointers will be greatly appreciated

Thanks in advance,
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Just pinging to see if any one could help me with this problem

Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Did you fill in the proxy settings in the code correctly? Because in your code sample you have filled in :

System.getProperties().put("proxyHost", "proxyname");
System.getProperties().put("proxyPort", "proxyyport");

Maybe you should take a look at:
Andrew Moore

Joined: Mar 07, 2006
Posts: 1
I had this exact same problem.
The issue lies around the authentiction. I tried the follwoing:
System.getProperties().put("http.proxyUser", "myuser");
System.getProperties().put("http.proxyPassword", "mypass");
but it didn't work.

I eventually found the fix whilst polling through forums.
Create a new class along the lines of teh following:
import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class httpAuthProxy extends Authenticator {

protected PasswordAuthentication setAuthentication() {
return new PasswordAuthentication("myUser","myPass".toCharArray());

Then, in your code add the following:

System.getProperties().put("http.proxyHost", "myHost");
System.getProperties().put("http.proxyPort", "8080");
Authenticator.setDefault( new httpAuthProxy() );

Hope this helps
I agree. Here's the link: http://aspose.com/file-tools
subject: accessing URL through java from behind a fire wall: