How to call a webservice through a proxy server with java (axis)?

Discussion in 'Java' started by contrex, Apr 13, 2004.

  1. contrex

    contrex Guest

    I wrote a simple webservice client in java (Axis). When I call a
    webservice localized in my local network all works fine but when I
    call external webservices I get a java.net.UnknownHostException. I
    think it's because I have to use a proxy when I want to connect to the
    internet but I don't know how to tell it my java client.

    Other java classes (not calling webservices) use the Socket class in
    order to connect through a proxy to the internet which works very
    well:

    #### BEGIN code snippet
    URL server = new URL("http://foo.bar");
    Socket socket = new Socket("UrlProxy", 1234);
    Writer writer = new OutputStreamWriter(socket.getOutputStream(),
    "US-ASCII");
    writer.write("GET " + server.toExternalForm() + " HTTP/1.0\r\n");
    writer.write("Host: " + server.getHost() + "\r\n");
    writer.write("Proxy-Authorization: Basic "
    + new sun.misc.BASE64Encoder().encode("someAuthenticationString".getBytes())
    + "\r\n\r\n");
    writer.flush();
    BufferedReader reader = new BufferedReader(new
    InputStreamReader(socket.getInputStream(),"US-ASCII"));
    #### END code snippet

    But in the axis examples of the java client I didn't find a Socket
    class or other things where I could specify my proxy.

    Can you tell me please how the java client have to call a webservice
    through a proxy server? THANKS!

    Here is the code of my java client:

    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    import org.apache.axis.encoding.XMLType;
    import javax.xml.rpc.ParameterMode;
    import javax.xml.namespace.QName;

    public class TestClient
    {
    public static void main(String [] args) {
    try {

    String endpoint =
    "http://www.vbnetexpert.com/vsm/timeservice";

    Service service = new Service();
    Call call = (Call) service.createCall();

    call.setTargetEndpointAddress( new java.net.URL(endpoint) );
    call.setOperationName("GetLocal");

    String ret = (String) call.invoke( new Object[] { "foo" } );
    System.out.println("Sent 'foo', got '" + ret + "'");

    }catch (Exception e) {
    System.err.println(e.toString());
    }
    }
    }
     
    contrex, Apr 13, 2004
    #1
    1. Advertising

  2. Hi....
    There are 2 ways to do that!
    You can set the proxy by using startup params (for http):
    java ...... -Dhttp.proxySet=true -Dhttp.proxyHost=proxy.xxxx.com -Dhttp.proxyPort=8080
    or for general (for all protocols):
    java ...... -DproxySet=true -DproxyHost=proxy.xxxx.com -DproxyPort=8080
    or in the code (what may be is easier for you by using axis):
    ....
    Properties props= new Properties(System.getProperties());
    props.put("http.proxySet", "true");
    props.put("http.proxyHost", "proxy.xxxx.com");
    props.put("http.proxyPort", "8080");
    Properties newprops = new Properties(props);
    System.setProperties(newprops);
    ....
    Enjoy....
    Greetz,
    Dani

    "contrex" <> wrote in message
    news:...
    > I wrote a simple webservice client in java (Axis). When I call a
    > webservice localized in my local network all works fine but when I
    > call external webservices I get a java.net.UnknownHostException. I
    > think it's because I have to use a proxy when I want to connect to the
    > internet but I don't know how to tell it my java client.
    >
    > Other java classes (not calling webservices) use the Socket class in
    > order to connect through a proxy to the internet which works very
    > well:
    >
    > #### BEGIN code snippet
    > URL server = new URL("http://foo.bar");
    > Socket socket = new Socket("UrlProxy", 1234);
    > Writer writer = new OutputStreamWriter(socket.getOutputStream(),
    > "US-ASCII");
    > writer.write("GET " + server.toExternalForm() + " HTTP/1.0\r\n");
    > writer.write("Host: " + server.getHost() + "\r\n");
    > writer.write("Proxy-Authorization: Basic "
    > + new sun.misc.BASE64Encoder().encode("someAuthenticationString".getBytes())
    > + "\r\n\r\n");
    > writer.flush();
    > BufferedReader reader = new BufferedReader(new
    > InputStreamReader(socket.getInputStream(),"US-ASCII"));
    > #### END code snippet
    >
    > But in the axis examples of the java client I didn't find a Socket
    > class or other things where I could specify my proxy.
    >
    > Can you tell me please how the java client have to call a webservice
    > through a proxy server? THANKS!
    >
    > Here is the code of my java client:
    >
    > import org.apache.axis.client.Call;
    > import org.apache.axis.client.Service;
    > import org.apache.axis.encoding.XMLType;
    > import javax.xml.rpc.ParameterMode;
    > import javax.xml.namespace.QName;
    >
    > public class TestClient
    > {
    > public static void main(String [] args) {
    > try {
    >
    > String endpoint =
    > "http://www.vbnetexpert.com/vsm/timeservice";
    >
    > Service service = new Service();
    > Call call = (Call) service.createCall();
    >
    > call.setTargetEndpointAddress( new java.net.URL(endpoint) );
    > call.setOperationName("GetLocal");
    >
    > String ret = (String) call.invoke( new Object[] { "foo" } );
    > System.out.println("Sent 'foo', got '" + ret + "'");
    >
    > }catch (Exception e) {
    > System.err.println(e.toString());
    > }
    > }
    > }
     
    Daniel Albisser, Apr 13, 2004
    #2
    1. Advertising

  3. contrex

    contrex Guest

    Thanks for your help! You pointed me in the right direction but my
    java client is still unable to reach the webservice. My proxy needs
    authentication so I added 2 lines to your code:

    System.getProperties().setProperty("http.proxyUser", "myUserId");
    System.getProperties().setProperty("http.proxyPassword", "myPwd");

    When I execute the java client I get:
    (405)Method Not Allowed

    If I encode userId and password as I read in some articles as shown
    here:
    String userid = new sun.misc.BASE64Encoder().encode("myUserId".getBytes());
    String password = new
    sun.misc.BASE64Encoder().encode("myPwd".getBytes());
    System.getProperties().setProperty("http.proxyUser", userid);
    System.getProperties().setProperty("http.proxyPassword", password);

    ....then I get:
    (407)Proxy Authentication Required


    So, I read some more articles as (i.e.
    http://www.doc.ic.ac.uk/~ca99/blogs/000157.html) and I even used the
    Authenticator class as described there. The error I get then is:
    (407)Proxy Authentication Required

    Really strange is that the method "getPasswordAuthentication()" is
    NEVER called! Why?

    Ok, at least the first aproach seems to use my proxy correctly but why
    I get an "(405)Method Not Allowed" error?

    Thanks for your advice!


    Here is the code of my java client:

    public class TestClient{

    public static void main(String [] args) {

    TestClient tc = new TestClient();
    //tc.callInternalWebservice();
    tc.callExternalWebservice();
    }

    public void callExternalWebservice() {
    try {

    System.getProperties().setProperty("http.proxySet", "true");
    System.getProperties().setProperty("http.proxyHost",
    "proxy.com");
    System.getProperties().setProperty("http.proxyPort", "8080");

    // NOW CREATE HTTP AUTHENTICATION
    // ...either by setting system props
    String userid = "myUserId";
    String password = "myPwd";
    //userid = new
    sun.misc.BASE64Encoder().encode(userid.getBytes());
    //password = new
    sun.misc.BASE64Encoder().encode(password.getBytes());
    System.getProperties().setProperty("http.proxyUser", userid);
    System.getProperties().setProperty("http.proxyPassword",
    password);

    // ...or by using Authenticator class (Java2)
    //Authenticator.setDefault(new MyAuthenticator());

    String endpoint =
    "http://www.vbnetexpert.com/vsm/timeservice";

    Service service = new Service();
    Call call = (Call) service.createCall();

    call.setTargetEndpointAddress( new java.net.URL(endpoint) );
    call.setOperationName("GetLocal");

    String ret = (String) call.invoke( new Object[] { "foo" } );
    System.out.println("Sent 'foo', got '" + ret + "'");


    }catch (Exception e) {
    System.err.println(e.toString());
    }
    }

    public class MyAuthenticator extends Authenticator {

    protected PasswordAuthentication getPasswordAuthentication() {
    System.out.println(">>>getPasswordAuthentication");//NEVER
    called! Why?
    return new PasswordAuthentication("myUserId",
    "myPwd".toCharArray());
    }
    }
    }
     
    contrex, Apr 14, 2004
    #3
  4. contrex

    Rogan Dawes Guest

    contrex wrote:

    > Thanks for your help! You pointed me in the right direction but my
    > java client is still unable to reach the webservice. My proxy needs
    > authentication so I added 2 lines to your code:
    >
    > System.getProperties().setProperty("http.proxyUser", "myUserId");
    > System.getProperties().setProperty("http.proxyPassword", "myPwd");
    >
    > When I execute the java client I get:
    > (405)Method Not Allowed
    >
    > If I encode userId and password as I read in some articles as shown
    > here:
    > String userid = new sun.misc.BASE64Encoder().encode("myUserId".getBytes());
    > String password = new
    > sun.misc.BASE64Encoder().encode("myPwd".getBytes());
    > System.getProperties().setProperty("http.proxyUser", userid);
    > System.getProperties().setProperty("http.proxyPassword", password);


    Note that the header that is sent to the proxy as part of the request
    looks like:

    Proxy-authenticate: Basic BASE64EncodedString

    where BASE64EncodedString is Base64("username" + ":" + "password");

    Consequently, I think it unlikely that you need to supply an encoded
    username and an encoded password separately to the System properties.

    Maybe try supplying the username and password as a plain string, without
    encoding it?

    Rogan
    --
    Rogan Dawes
    nntp_AT_dawes*DOT*za-DOT-net
     
    Rogan Dawes, Apr 15, 2004
    #4
  5. contrex

    contrex Guest

    Rogan, yes you are right. If I don't encode it works. I just had to
    change to another webservice because it seems that the webservice was
    disabled (therefore the error "(405)Method Not Allowed" I suppose).
    The new webservice I use sends a correct response
    (http://ww6.borland.com/webservices/MapQuest/MapQuest.exe/soap/IMapQuest).

    However, if I use the Authenticator class I get still the error
    "(407)Proxy Authentication Required" and the method
    "getPasswordAuthentication()" is never called! This is strange to me
    and I read that other peoples had problems using the Authenticator
    class.
    Somebody knows what's wrong with it?

    If not I'll use this solution which works fine for me but which has
    some drawbacks i.e. when we want to use multiple connections having
    different proxy-configs for the same application:
    System.getProperties().setProperty("http.proxyUser", userid);
    System.getProperties().setProperty("http.proxyPassword", password);


    Thanks again for your help!
     
    contrex, Apr 16, 2004
    #5
  6. contrex

    thebigj816

    Joined:
    Jul 23, 2011
    Messages:
    1
    set proxy with http URL class object

    You can set proxy while creating URL class object (since java 5)-
    URL u = new URL("<url address>", new Proxy(new InetAddress("proxyserverIPaddressorurl", proxyPortNumber)));

    proxy class also has provision for user name and password for proxy server.



     
    thebigj816, Jul 23, 2011
    #6
    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. Replies:
    0
    Views:
    964
  2. Cyril
    Replies:
    2
    Views:
    4,540
    Cyril
    Jun 1, 2006
  3. JD
    Replies:
    5
    Views:
    294
    Lalitha
    Oct 7, 2004
  4. Dermot O'Loughlin

    Problem accessing WebService through Proxy Server

    Dermot O'Loughlin, Dec 1, 2004, in forum: ASP .Net Web Services
    Replies:
    4
    Views:
    538
    Dan Rogers
    Dec 2, 2004
  5. Replies:
    2
    Views:
    775
Loading...

Share This Page