Link to home
Start Free TrialLog in
Avatar of batdan
batdan

asked on

Using sockets from behind a proxy server

Hi.

I'm having trouble using a Socket class to connect to another server when I'm behind our corporate proxy server.  I'm getting an Exception detailing a "java.net.NoRouteToHostException: No route to host: connect" error.

The code I have is:


import java.io.*;
import java.net.*;
import util.*;

public class SocketTest
{
    public static void main(String[] args)
    {
        try
        {
            System.setProperty( "proxySet", "true" );
            System.setProperty( "http.proxyHost", "xx.xx.xx.xxx" );
            System.setProperty( "http.proxyPort", "xxxx" );

            /*
            URL url=new URL("http://xx.xx.xx.xxx");
            URLConnection uc = url.openConnection ();
            String encoded = new String(base64Encode(new String("userName:password")));
            uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
            uc.connect();
             */

            System.setProperty( "http.proxyUser", "userName" );
            System.setProperty( "http.proxyPassword", "password" );
           
            Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
           
            BufferedReader in = new BufferedReader(
                new InputStreamReader(s.getInputStream()));
           
            boolean more = true;
           
            while(more)
            {
                String line = in.readLine();
                if (line == null)
                    more = false;
                else
                    System.out.println(line);
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

Can't seem to get the thing to work.  I don't know if it's of any relevence but just spoke to an IT guy who said that the proxy server acts an authentication server..the actual websweeper box is on NT, where all the filtering happens.  I'm not too clued up on all of this proxy/firewall stuff so can anyone suggest anything I'm doing wrong?

Thanks in advance

batdan
Avatar of Ovi
Ovi

You have a normal http connection thru browsers outside the proxy? If yes, requires authentication?
Avatar of girionis
 Can you try using this and tell us what happens?

Properties props = System.getProperties();
props .put("proxySet", true);
props .put("proxyHost", <whatever>);
props .put("proxyPort", <port>);
System.setProperties(props);
Avatar of batdan

ASKER

Hi,

Ovi:
Normally I use a browser to connect to the web, which prompts me for a username and password.  Whichever method I use (browser or java Sockets) I will have to go through the proxy.

girionis:
I've tried that already but still has the same end result.

Thanks for the input....
System.setProperty( "http.proxyUser", "userName" );
System.setProperty( "http.proxyPassword", "password" );
           
Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
           
...

The proxy settings are used for establishen http conntection through the proxy. If you want plain socket connection - you will have to use SOCKS protocol, or your proxy (not with setting system propeties - but dirctly connecting to it) by sending a CONNECT request to it
Avatar of batdan

ASKER

Hi Venci75,

I'm not sure that I understood entirely your advice (I'm a bit of a novice at this lark).

If I connect directly to the proxy, would this equate roughly to the code that's commented out of my first post?  I've tried using this but am getting the same response, but maybe the code is not quite right or I'm missing something out.

I don't think I can use the SOCKS protocol, but I'll have to go through the proxy in any case.

Thanks
Socket s = new Socket("proxy", proxyPort);
OutputStream out = s.getOutputStream();
out.write("CONNECT host:port HTTP/1.1\r\n");
String encoded = new String(base64Encode(new String("userName:password")));
out.write("Proxy-Authorization: Basic " + encoded);
out.write("\r\n\r\n");
out.flush();

InputStream in = s.getInputStream();
// read the proxy http response here            
Avatar of batdan

ASKER

Hi Venci75.

Sorry for the delay every time you post, I'm working on too many projects at once!

If I then run this, which compiles OK:

    public static void main(String[] args)
    {
        try
        {
            Socket s = new Socket("proxy", port);
            OutputStream t = s.getOutputStream();
            DataOutputStream out = new DataOutputStream(t);
            out.writeUTF("CONNECT time-A.timefreq.bldrdoc.gov:13 HTTP/1.1\r\n");
            String encoded = new String(base64Encode(new String("usrname:pwd")));
            out.writeUTF("Proxy-Authorization: Basic " + encoded);
            out.writeUTF("\r\n\r\n");
            out.flush();
           
            BufferedReader in = new BufferedReader(
                new InputStreamReader(s.getInputStream()));
            System.out.println(in.readLine());
        }
             
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

...then I get the error:

java.net.SocketException: Cannot send after socket shutdown: JVM_recv in socket input stream read

when the in.readLine() method is called.

Do you know what this might be?

Thanks for the continued help.
probably you close some/all of the socket, output streams or readers
Avatar of batdan

ASKER

I'm running exactly the code I've included in the last post, so you can see that I'm not explicitly closing either socket or i/o stream (I had to use a DataOutputStream o write to the socket, but I can't see this being a problem).  I'll keep trying.
ASKER CERTIFIED SOLUTION
Avatar of Venci75
Venci75

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of batdan

ASKER

Ah, thanks, I now have the output:

HTTP/1.0 403 Forbidden
Proxy-agent: Netscape-Proxy/3.5
Date: Tue, 01 Apr 2003 10:09:21 GMT
Content-type: text/html
Content-length: 182

<HTML><HEAD><TITLE>Forbidden</TITLE></HEAD>
<BODY><H1>Forbidden</H1>
The proxy's access control configuration denies access to
the requested object through this proxy.
</BODY></HTML>


Typical!  Not sure why this is forbidden as when I try connect to a web site that I use through a browser all the time, the same message is brought up.  For that matter, if I change the host to an invalid url, then I also get the same 'forbidden' output, so it doesn't look like it's even getting to the stage where the request is being processed.

If you have any idea why this might be happening then I'd be grateful for any input.  I will award you the points for this question for your help so far, as effectively I now have a socket connecting to the proxy.  I can always open another question regarding this new problem if necessary.

I'll have a play around though for the time being to try & get this working.
I suspect that the proxy is configured to reject these requests. Can you try the same - but connecting to
www.yahoo.com:80
? If this is working - then surely the proxy does not like the address. Otherwise - probably the problem is with the proxy authorization
Avatar of batdan

ASKER

I get the same 'Forbidden' message when using the yahoo address as well.  I'll check the code for the base64Encode method to make sure that it's encoding properly, but I'm fairly sure it's working as it should.
batdan:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Venci75's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
 ok :)
Avatar of batdan

ASKER

My sincere apologies for ignoring this Venci75 , I got laid off from the job & kind of forgot about the question as a result...tsk.

Thanks for all the info, if you're still around. ;)
Thank you batdan.  At least I got the decision right ;-)