Solved

How to proxy server throw java program

Posted on 2011-03-04
21
586 Views
Last Modified: 2012-05-11


Hi ,

How to set proxy server throw java program .

I tried with system.property() and proxy class not use ?

is it any other way ??
0
Comment
  • 8
  • 8
  • 4
  • +1
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 225 total points
ID: 35035738
http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html

See proxyHost and proxyPort. What did you do and for what purpose?
0
 
LVL 92

Expert Comment

by:objects
ID: 35035771
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35035775
>>>>>>>t. What did you do and for what purpose?

For web services call , Hvaing problem with firewall so that only i want to set proxy !
System.getProperties().put("http.proxyHost", "xxxx");

i alerday use that link its not working ..... :(
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35035786
System.setProperty("http.proxyHost", xxxx);
0
 
LVL 92

Assisted Solution

by:objects
objects earned 225 total points
ID: 35035788
depends what you are using to make connection, many don't use it.

httpclient has a setProxy() method you can use
http://helpdesk.objects.com.au/java/how-to-get-httpclient-to-use-a-proxy-server
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35035794
Make sure the proxy is running and accepting connections
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35035845
How to check that ??
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 225 total points
ID: 35035851
Try


telnet <address> <port>
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35035868
Hostconfig() not avaliable in HttpClient ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35035882
Are you actually using HttpClient?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35035956
Ya its connecting .... i tried in cmd promt it connect blank screen is coming
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35035991
It might be worth making a little test app to make a simple http request through the proxy, if only to check that it works properly and doesn't require authentication (which you'd have to implement)
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35035995
is that correct ??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35036031
Is what correct?
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35036102
Ya its connecting .... i tried in cmd promt it connect blank screen is coming


is that correct ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35036194
Yes it should indicate that it's running but i'd follow my last suggestion
0
 
LVL 92

Assisted Solution

by:objects
objects earned 225 total points
ID: 35036196
see the following if you need authentication
http://helpdesk.objects.com.au/java/how-to-specify-authentication-details-for-proxy-server

I'd suggest using httpclient to make the connection, it provides good proxy support
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 35036394
@chj : HOw to test that ??

@Object : getHiostConfig() not avaliable ??
0
 
LVL 1

Assisted Solution

by:GenroseSusai
GenroseSusai earned 50 total points
ID: 35037955
Connect through a Proxy

The Networking Properties

You have to set the following properties :
http.proxyHost (default: <none>)
http.proxyPort (default: 80 if http.proxyHost specified)
http.nonProxyHosts (default: <none>)
NOTE: proxyHost, proxyPort are deprecated. you have to prefix them with "http.".
NOTE: Those properties are documented here : http://java.sun.com/javase/6/docs/technotes/guides/net/properties.html.
You can set the required properties when starting the JVM for a JAVA application from the command line:

java  -Dhttp.proxyHost=myproxyserver.com  -Dhttp.proxyPort=80 MyJavaApp
Or in your source :
System.setProperty("http.proxyHost", "myProxyServer.com");
System.setProperty("http.proxyPort", "80");
Since Java 1.5 you can also pass a java.net.Proxy instance to the openConnection() method:
//Proxy instance, proxy ip = 123.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.0.0.1", 8080));
URL url = new URL("http://www.yahoo.com");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
       
String page;
StringBuffer tmp = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((line = in.readLine()) != null){
   page.append(line + "\n");
}
System.out.println(page);
so you don't need to set system properties.
You can use the default PROXY as defined by your networking settings.

System.setProperty("java.net.useSystemProxies", "true");
List l = null;
try {
  l = ProxySelector.getDefault().select(new URI("http://www.yahoo.com"));
}
catch (URISyntaxException e) {
  e.printStackTrace();
}

if (l != null) {
   for (Iterator iter = l.iterator(); iter.hasNext() {
      java.net.Proxy proxy = (java.net.Proxy) iter.next();
      System.out.println("proxy hostname : " + proxy.type());
      InetSocketAddress addr = (InetSocketAddress) proxy.address();
      if (addr == null) {
        System.out.println("No Proxy");
      }
      else {
        System.out.println("proxy hostname : " + addr.getHostName());
        System.out.println("proxy port : " + addr.getPort());
      }
   }
}
To bypass the PROXY,
URL url = new URL("http://internal.server.local/");
URLConnection conn = url.openConnection(Proxy.NO_PROXY);
Proxy and Username/Password

You might need to identify yourself to the proxy server.

One way is to use the HTTP property "Proxy-Authorization" with a username:password base64 encoded.

System.setProperty("http.proxyHost", "myProxyServer.com");
System.setProperty("http.proxyPort", "80");
URL url=new URL("http://someserver/somepage");
URLConnection uc = url.openConnection ();
String encoded = new String
      (Base64.base64Encode(new String("username:password").getBytes()));
uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
uc.connect();
NOTE: For a base64 function, see this How-to.
The following example dumps the content of a URL but before we identify ourself to the proxy.
import java.net.*;
import java.io.*;

public class URLUtils {
  public static void main(String s[]) {
    URLUtils.dump("http://www.yahoo.com");
    System.out.println("**************");
    URLUtils.dump("https://www.paypal.com");
    System.out.println("**************");
  }

  public static void dump(String URLName){
    try {
      DataInputStream di = null;
      FileOutputStream fo = null;
      byte [] b = new byte[1];

      // PROXY
      System.setProperty("http.proxyHost","proxy.mydomain.local") ;
      System.setProperty("http.proxyPort", "80") ;

      URL u = new URL(URLName);
      HttpURLConnection con = (HttpURLConnection) u.openConnection();
      //
      // it's not the greatest idea to use a sun.misc.* class
      // Sun strongly advises not to use them since they can
      // change or go away in a future release so beware.
      //
      sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
      String encodedUserPwd =
         encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
      con.setRequestProperty
         ("Proxy-Authorization", "Basic " + encodedUserPwd);
      // PROXY ----------

      di = new DataInputStream(con.getInputStream());
      while(-1 != di.read(b,0,1)) {
         System.out.print(new String(b));
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
With JDK1.2, the java.net.Authenticator can be used to send the credentials when needed.
public static void dump(String URLName){
  try {
    DataInputStream di = null;
    FileOutputStream fo = null;
    byte [] b = new byte[1];
 
    // PROXY
    System.setProperty("http.proxyHost","proxy.mydomain.local") ;
    System.setProperty("http.proxyPort", "80") ;
 
    Authenticator.setDefault(new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new
           PasswordAuthentication("mydomain\\username","password".toCharArray());
    }});
 
    URL u = new URL(URLName);
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    di = new DataInputStream(con.getInputStream());
    while(-1 != di.read(b,0,1)) {
       System.out.print(new String(b));
    }
  }
  catch (Exception e) {
          e.printStackTrace();
  }
}
Bypass a Proxy

In intranet environment, you may need to bypass the proxy server and go directly to the http server.

The http.nonProxyHosts property indicates the hosts which should be connected too directly and not through the proxy server. The value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can be used for matching.

java.exe  
   -Dhttp.nonProxyHosts="*.mycompany.com|*.mycompany.local|localhost"
      MyClass
0
 
LVL 92

Expert Comment

by:objects
ID: 35040903
> @Object : getHiostConfig() not avaliable ??

should be getHostConfiguration()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35082347
:)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now