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

C

contrex

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());
}
}
}
 
D

Daniel Albisser

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
 
C

contrex

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());
}
}
}
 
R

Rogan Dawes

contrex said:
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
 
C

contrex

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!
 
Joined
Jul 23, 2011
Messages
1
Reaction score
0
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.



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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top