Accessing Web Service over SSL

S

Syypher

I created a web service on my Tomcat server and utilized some sample
code I found to access the web server and retrieve data. However,
after moving the web service to be utilized over SSL, things went awry.
The code I used for accessing it is as follows:

String endpoint =
"http://localhost:8080/piApp/services/DynamicRatings";

Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( "getRatings" );
call.setReturnType( XMLType.XSD_STRING );
String retVal = (String) call.invoke( new Object [] { });
out.println(retVal);
}
catch (ServiceException s)
{

}

The problem is that java.net.URL doesn't seem to support https. So,
I'm wondering if anyone can point me to some sample code for accessing
a web service over ssl. Any help would be greatly appreciated.
Regards,
Lee
 
S

Syypher

I looked through the Jakarta Commons HttpClient package, and it doesn't
appear to be what I'm looking for.

I'm using org.apache.axis.client.Call to make the call to the web
service, and it's setTargetEndpointAddress() method requires a
java.net.URL object. I couldn't find anything in Jakarta Commons that
extended java.net.URL and implemented SSL.

In the event that you were pointing me there as a means of utilizing
Jakarta Commons HttpClient to call my web service, I continued to plow
through the docs to find something that would allow me to call a web
service, but I didn't see anything. Granted, it's very much possible
that I completely skimmed right over it.
 
W

Wendy S

Syypher said:
I'm using org.apache.axis.client.Call to make the call to the web
service, and it's setTargetEndpointAddress() method requires a
java.net.URL object. I couldn't find anything in Jakarta Commons that
extended java.net.URL and implemented SSL.

Sorry... in my limited web services experience, I've always been able to
talk to them by over HTTP. I would POST the information to the web service,
then receive the xml-formatted HTTP response.
 
S

Syypher

I see what you're saying. Would you be able to provide an example of
the post that you use to access a web service?
 
N

Neill

Syypher said:
I see what you're saying. Would you be able to provide an example of
the post that you use to access a web service?

I'm using this code to work with UPS Online Tools. I realize it's not
recommended to use sun packages, but that's more of a political discussion
than practical. If anyone has an alternative in this case, please share. -
Neill
--

Neill Laney
http://www.laneyconsulting.com

protected void RSSRequest() throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLSocketFactory factory =
(SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(rssHost, 443);
//SSLSocket socket = (SSLSocket) factory.createSocket(RSS, 443);
/*
* send http request
*
* Before any application data is sent or received, the
* SSL socket will do SSL handshaking first to set up
* the security attributes.
*
* SSL handshaking can be initiated by either flushing data
* down the pipe, or by starting the handshaking by hand.
*
* Handshaking is started manually in this example because
* PrintWriter catches all IOExceptions (including
* SSLExceptions), sets an internal error flag, and then
* returns without rethrowing the exception.
*
* Unfortunately, this means any error messages are lost,
* which caused lots of confusion for others using this
* code. The only way to tell there was an error is to call
* PrintWriter.checkError().
*/
socket.startHandshake();
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())));
/*
* write XML to out
*/
out.write("POST /ups.app/xml/Rate HTTP/1.1");
out.write("Host: ".concat(rssHost));
//out.write("Content-Type: text/xml");
out.write("");
// out.println();
// out.flush();
/*
out.println("GET /ups.app/xml/Rate HTTP/1.1");
out.println();
out.flush();
*/
/*
* Make sure there were no surprises
*/
if (out.checkError())
System.out.println("SSLSocketClient: java.io.PrintWriter error");
/* read response */
BufferedReader in =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
/*
* write to XML parser and set Data Object values
*/
System.out.println(inputLine);
in.close();
out.close();
socket.close();
}
 
C

Chris Smith

Syypher said:
The problem is that java.net.URL doesn't seem to support https.

That's not true, at least for Java 1.4 and up. Can you post sample code
to demonstrate the problem? Also, what happens if you add a call to
javax.net.ssl.SSLContext.init(null, null, null) before trying to create
the URL?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top