aspose file tools*
The moose likes Web Services and the fly likes Invoking a https webservice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Invoking a https webservice" Watch "Invoking a https webservice" New topic
Author

Invoking a https webservice

Ansari Raffik
Greenhorn

Joined: Feb 05, 2007
Posts: 7
I am new to web service Can anyone give me a sample code for invoking https protocol enabled web service
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
Welcome to JavaRanch.

This introduction to SAAJ talks about clients and services that use HTTPS, particularly using Tomcat as the container (towards the end of the document).


Ping & DNS - my free Android networking tools app
Ansari Raffik
Greenhorn

Joined: Feb 05, 2007
Posts: 7
Can I use a simple http call in to invoke a https web service.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
For accessing an HTTPS URL an HTTP call will not suffice. How are you developing the client - are you actually using the java.net.HttpUrlConnection class? Then it might be as simple as replacing that with the javax.net.ssl.HttpsUrlConnection class.
Ansari Raffik
Greenhorn

Joined: Feb 05, 2007
Posts: 7
Yes I am using the java.net.HttpURLConnection class. In addition the service which i am calling has some digital certificates enabled.In that case will i have to make any configurations or modifications in my client.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
What do you mean by "certificates enabled"? The server/service will need to have at least one certificate installed -as does the client-, or else the SSL connection will not work. Do you want to retrieve the server certificates on the client and do something with them?
Ansari Raffik
Greenhorn

Joined: Feb 05, 2007
Posts: 7
Actually the scenario is like the client(say A) have to access a https url(say B) by passing some string.They have provided me the keystore file and with that i have to access the https url.B will actually validate the request which iam sending with the help of keystore and then if it ok B will call a web service (say C).

I just need a sample of the client to access the https url which is protected by a digital certificate(key pair value which has a public and private key. The public key is the one that is provided by B which is the keystore file)

sorry I may be poor in explaining things.

Thanks a lot...
Michael Dekmetzian
Greenhorn

Joined: Nov 25, 2004
Posts: 10
I'm having similar issues at the moment, so hopefully by explaining my issue you get some traction too.

I'm new at web services stuff, but have built a simple client that will connect and get info from non-password protected http webservice.

What i need to do is connect to a password protected https webservice.

I've created all the stubs using WDSL2Java by downloading the WSDL then running :



Which all works perfectly.

My test http client is this :



Which works fine. I'm trying to adapt this code to work with a https service ..



I'ts bombed out in the /** **/ section. Nb 'Service' is a generated class stub and not part of any of the API's.

I've added the certificate to the store using:



And that works fine. So i believe i now have access to the webservice over https.

I'm now getting a http error 401 error as the web service also requires that i authenticate with a username/password.. which i can't work out how to do.

Any thoughts ?



-Mikey

[ March 28, 2007: Message edited by: Michael Dekmetzian ]
[ March 28, 2007: Message edited by: Michael Dekmetzian ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
Using HTTP authentication in WS is demonstrated here.
Michael Dekmetzian
Greenhorn

Joined: Nov 25, 2004
Posts: 10
But it bombs out when creating the service, so i'm not sure where this code goes



I don't have a method to call at that point (as all i have is the ServiceFactory) ... i'm not sure where this info would go?

Thanks for the info regardless though
Michael Dekmetzian
Greenhorn

Joined: Nov 25, 2004
Posts: 10
Or are you saying i should utilise the SAAJClient in "What is the simplest possible Java client for a web service?" ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
What does "bomb out" mean - is there an exception? If so, what is it?
Michael Dekmetzian
Greenhorn

Joined: Nov 25, 2004
Posts: 10
Also just found this :



Here: http://www-128.ibm.com/developerworks/webservices/library/ws-sec1.html

The main thing i don't get about that one is how this connects with your 'stub' classes which define how you interact with the webservice... or do you not use them with these methods because you're using call.invoke?
Michael Dekmetzian
Greenhorn

Joined: Nov 25, 2004
Posts: 10
Hi Ulf,

Bomb out - as per my first post, i'm getting a 401 error because i haven't authenticated. Previously when i didn't have the certificate installed, it wasn't letting me connect.

Cheers

-Michael
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
The main thing i don't get about that one is how this connects with your 'stub' classes which define how you interact with the webservice... or do you not use them with these methods because you're using call.invoke?

Yes, you wouldn't use the stub classes - that code contains everything that's needed. It uses a different kind of API, which is dependent on Axis.

i'm getting a 401 error because i haven't authenticated.

String wsdlLoc = "https://SOMEURL/SalesServer/Service.asmx?WSDL";

QName serviceName = new QName("http://SOMURL/webservices", "Service");

ServiceFactory sFactory = ServiceFactory.newInstance( );

/**BOMBS OUT ON THIS LINE **/
javax.xml.rpc.Service service = sFactory.createService(new URL(wsdlLoc), serviceName);


In that case, save the WSDL to a local file, and then use a file:/// URL in the call to createService.
Michael Dekmetzian
Greenhorn

Joined: Nov 25, 2004
Posts: 10
If i'm creating the service from a file (which does work), how does it know where the server actually exists?

The target namespace isn't the url of the dev server i'm connecting to.
Michael Dekmetzian
Greenhorn

Joined: Nov 25, 2004
Posts: 10
Worked it out, you use the local file to load the wdsl, but define an endpoint in the stubs



Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
Hi Guys,

Can you please answer some very basic questions of mine.

1) The WSDL location specified in the generated stubs, must be accessible from the client's machine. (What ever the case may be). Am i right?
2) The end point must be hittable from the client's machine.

Please let me know if the above 2 points I am saying are correct?

Actually, we have a WSDL posted on a HTTPS url. And the client says, they have protected it. And we are not able to open it in our browser.
So, can that be a reason I am not able to invoke the web service methods?

Regards
Yogesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoking a https webservice