Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to call a webservice through a proxy server with java (axis)?

Reply
Thread Tools

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

 
 
contrex
Guest
Posts: n/a
 
      04-13-2004
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("someAuthenticatio nString".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());
}
}
}
 
Reply With Quote
 
 
 
 
Daniel Albisser
Guest
Posts: n/a
 
      04-13-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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("someAuthenticatio nString".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());
> }
> }
> }


 
Reply With Quote
 
 
 
 
contrex
Guest
Posts: n/a
 
      04-14-2004
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.proxyPass word", "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".getByte s());
String password = new
sun.misc.BASE64Encoder().encode("myPwd".getBytes() );
System.getProperties().setProperty("http.proxyUser ", userid);
System.getProperties().setProperty("http.proxyPass word", 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.proxyPass word",
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());
}
}
}
 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      04-15-2004
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.proxyPass word", "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".getByte s());
> String password = new
> sun.misc.BASE64Encoder().encode("myPwd".getBytes() );
> System.getProperties().setProperty("http.proxyUser ", userid);
> System.getProperties().setProperty("http.proxyPass word", 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
 
Reply With Quote
 
contrex
Guest
Posts: n/a
 
      04-16-2004
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/M...oap/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.proxyPass word", password);


Thanks again for your help!
 
Reply With Quote
 
thebigj816 thebigj816 is offline
Junior Member
Join Date: Jul 2011
Posts: 1
 
      07-23-2011
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.



Quote:
Originally Posted by contrex View Post
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/M...oap/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.proxyPass word", password);


Thanks again for your help!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call WebService over https via proxy... koraykazgan Software 0 08-16-2007 03:20 PM
Help: Connecting to a password-protected webservice through a proxy server (ISA 2000) Hanz Gruuber ASP .Net Web Services 0 04-29-2005 04:58 AM
Problem accessing WebService through Proxy Server Dermot O'Loughlin ASP .Net Web Services 4 12-02-2004 08:34 PM
WebService Client not authenticating through Proxy Server JD ASP .Net Web Services 5 10-07-2004 09:58 PM
WebService on Java server versus WebService on .NET Mr. x Java 2 10-09-2003 11:21 PM



Advertisments