?
Solved

Using sockets from behind a proxy server

Posted on 2003-03-21
18
Medium Priority
?
862 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:batdan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +3
18 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 8180105
You have a normal http connection thru browsers outside the proxy? If yes, requires authentication?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8180110
 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);
0
 
LVL 1

Author Comment

by:batdan
ID: 8180181
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....
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Venci75
ID: 8180531
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
0
 
LVL 1

Author Comment

by:batdan
ID: 8211129
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
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8211616
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            
0
 
LVL 1

Author Comment

by:batdan
ID: 8238954
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.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8239137
probably you close some/all of the socket, output streams or readers
0
 
LVL 1

Author Comment

by:batdan
ID: 8244296
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.
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 1000 total points
ID: 8244343
try using writeBytes() instead of writeUTF():
...
out.writeBytes("CONNECT time-A.timefreq.bldrdoc.gov:13 HTTP/1.1\r\n");
...
0
 
LVL 1

Author Comment

by:batdan
ID: 8244586
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.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8244617
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
0
 
LVL 1

Author Comment

by:batdan
ID: 8244640
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.
0
 

Expert Comment

by:CleanupPing
ID: 9058560
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.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9730618
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
0
 
LVL 35

Expert Comment

by:girionis
ID: 9730726
 ok :)
0
 
LVL 1

Author Comment

by:batdan
ID: 9730759
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. ;)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9730864
Thank you batdan.  At least I got the decision right ;-)
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question