How to program with proxy

Discussion in 'Java' started by moonhk, Aug 11, 2006.

  1. moonhk

    moonhk Guest

    Below program worked before. How to program with proxy with input User
    name and password ?


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

    public class eproxy_tofile {
    public static void main(String[] args) {
    Properties prop = System.getProperties();
    prop.put("http.proxyHost","xx.xx.40.20");

    prop.put("http.proxyPort","8081");
    try {
    String Addr = "http://www.new-alliance.com/Naam_NAV.htm";

    URL yahoo = new URL(Addr);
    System.out.println("Conneting ... " + Addr);

    BufferedReader in = new BufferedReader(new InputStreamReader
    (yahoo.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) !=null)
    System.out.println(inputLine);
    in.close();
    }
    catch (Exception exc) {
    exc.printStackTrace();


    }

    }

    }
    moonhk, Aug 11, 2006
    #1
    1. Advertising

  2. moonhk wrote:
    > Below program worked before. How to program with proxy with input User
    > name and password ?


    Socks ?

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 12, 2006
    #2
    1. Advertising

  3. moonhk

    moonhk Guest

    Arne Vajhøj wrote:
    > moonhk wrote:
    > > Below program worked before. How to program with proxy with input User
    > > name and password ?

    >
    > Socks ?
    >
    > Arne


    I want daily or weekly download the contents of above site. Our Company
    using proxy. So need input proxy user id and password.
    moonhk, Aug 12, 2006
    #3
  4. moonhk wrote:
    > Arne Vajhøj wrote:
    >> moonhk wrote:
    >>> Below program worked before. How to program with proxy with input User
    >>> name and password ?

    >> Socks ?

    >
    > I want daily or weekly download the contents of above site. Our Company
    > using proxy. So need input proxy user id and password.


    I ask again: are you using socks ?

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 13, 2006
    #4
  5. moonhk

    EJP Guest

    moonhk wrote:

    > I want daily or weekly download the contents of above site. Our Company
    > using proxy. So need input proxy user id and password.


    Java.net.Authenticator
    EJP, Aug 13, 2006
    #5
  6. moonhk

    moonhk Guest

    Arne Vajhøj wrote:
    > moonhk wrote:
    > > Arne Vajhøj wrote:
    > >> moonhk wrote:
    > >>> Below program worked before. How to program with proxy with input User
    > >>> name and password ?
    > >> Socks ?

    > >
    > > I want daily or weekly download the contents of above site. Our Company
    > > using proxy. So need input proxy user id and password.

    >
    > I ask again: are you using socks ?
    >
    > Arne


    Sorry , What is socks ?
    moonhk, Aug 13, 2006
    #6
  7. moonhk wrote:
    > Sorry , What is socks ?


    That is a protocol that allows username/password
    for a proxy server.

    Which I do not think an ordinary HTTP proxy does.

    But we can change the question: how do you
    configure your browser to go out through that
    firewall ? And how does the browser get the
    username/password ?

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 13, 2006
    #7
  8. moonhk

    moonhk Guest

    Arne Vajhøj wrote:
    > moonhk wrote:
    > > Sorry , What is socks ?

    >
    > That is a protocol that allows username/password
    > for a proxy server.
    >
    > Which I do not think an ordinary HTTP proxy does.
    >
    > But we can change the question: how do you
    > configure your browser to go out through that
    > firewall ? And how does the browser get the
    > username/password ?
    >
    > Arne

    Checked with our Administrator, it is proxy server setting. Need to
    input username/password in Browser.
    moonhk, Aug 14, 2006
    #8
  9. moonhk wrote:
    > Checked with our Administrator, it is proxy server setting. Need to
    > input username/password in Browser.


    BASIC authentication ? (popup in browser ?)

    Try with:

    Authenticator.setDefault(new MyAuthenticator());

    where:

    class MyAuthenticator extends Authenticator {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("xxxx", "xxxx".toCharArray());
    }
    }

    but I think it is a bit unusual to use HTTP BASIC authentication
    with a proxy server.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 14, 2006
    #9
  10. moonhk

    moonhk Guest

    Arne Vajhøj wrote:
    > moonhk wrote:
    > > Checked with our Administrator, it is proxy server setting. Need to
    > > input username/password in Browser.

    >
    > BASIC authentication ? (popup in browser ?)
    >
    > Try with:
    >
    > Authenticator.setDefault(new MyAuthenticator());
    >
    > where:
    >
    > class MyAuthenticator extends Authenticator {
    > protected PasswordAuthentication getPasswordAuthentication() {
    > return new PasswordAuthentication("xxxx", "xxxx".toCharArray());
    > }
    > }
    >
    > but I think it is a bit unusual to use HTTP BASIC authentication
    > with a proxy server.
    >
    > Arne


    try. But not work. At home below java coding is worked.

    C:\Example\javaux\net>java eproxy_tofile
    Conneting ... http://www.new-alliance.com/Naam_NAV.htm
    java.io.IOException: Server returned HTTP response code: 500 for URL:
    http://www.new-alliance.com/Naam_NAV.htm
    at
    sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:800)
    at java.net.URL.openStream(URL.java:913)
    at eproxy_tofile.main(eproxy_tofile.java:53)

    C:\Example\javaux\net>



    /*

    eproxy_tofile.java
    2006/08/14 eric.leung


    Environment variable set to
    * CLASSPATH=c:\j2sdk1.4.1_01\lib\classes.zip;c:\example;.
    *
    */

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

    public class eproxy_tofile {
    public static void main(String[] args) {
    Properties systemSettings = System.getProperties();



    class MyAuthenticator extends Authenticator {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("xxx", "xxx".toCharArray());
    }

    }
    // String host,port,userid,passwd;



    systemSettings.put("proxySet","true");
    systemSettings.put("http.proxyHost",host);
    systemSettings.put("http.proxyPort",port);

    // System.setProperty("https.proxyUserName",userid);
    // System.setProperty("https.proxyPassword",passwd);

    System.setProperties(systemSettings);

    Authenticator.setDefault(new MyAuthenticator());


    try {

    String Addr = "http://www.new-alliance.com/Naam_NAV.htm";

    URL yahoo = new URL(Addr);
    System.out.println("Conneting ... " + Addr);

    BufferedReader in = new BufferedReader(new InputStreamReader
    (yahoo.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) !=null)
    System.out.println(inputLine);
    in.close();

    }
    catch (Exception exc) {
    exc.printStackTrace();


    }

    }

    }
    moonhk, Aug 14, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vidy5300
    Replies:
    0
    Views:
    813
    vidy5300
    Jun 7, 2004
  2. Replies:
    0
    Views:
    1,634
  3. Karsten Wutzke
    Replies:
    1
    Views:
    871
    Steven Simpson
    Aug 20, 2007
  4. Klein Stéphane
    Replies:
    3
    Views:
    585
    Steve Holden
    Dec 20, 2009
  5. Alejandro Molina

    Proxy and non Proxy configurations

    Alejandro Molina, Jul 15, 2004, in forum: ASP .Net Security
    Replies:
    0
    Views:
    140
    Alejandro Molina
    Jul 15, 2004
Loading...

Share This Page