This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Including information in the SOAP header in a web service invocation.   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Including information in the SOAP header in a web service invocation.   " Watch "Including information in the SOAP header in a web service invocation.   " New topic
Author

Including information in the SOAP header in a web service invocation.

Guadalupe Ortiz
Ranch Hand

Joined: Sep 12, 2003
Posts: 74
Hello,
I am creating web service clients in mobile devices thrpugh the use of JSR 172. I am aware it does not support SoapHandlers, bt I wonder if I can create the soap message "hard doding" it to include something in the soap header. What JSR would I need to use? ANy suggestion or example?

On the other hand, is there any difference in this scope in using CDC or CDLC?

Any suggestion is appreciated.
Thanks a lot,
Guadalupe
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
Yes, you can create soap message by hard "doding" complete message and write it to output stream.
Probably you have problem with authentication because username&pass goes to soap header.
Only thing that you need to know is how soap message looks like before it is sent to server. For that purpose you can create standard java ws client and catch message before it is sent.
Then in midlet you can create message as a string, open connection and write it to OutputStream as message.getBytes().
Jaclyn Tan
Greenhorn

Joined: Jan 16, 2009
Posts: 6
hi Aleksandar Babic,

you mentioned problems with authentication because username and password goes to the soap header?
i already tried implementing JSR172 and it worked successfully even with authentication.

i used setProperty() method of the Operation class and set Stub.USERNAME_PROPERTY and Stub.PASSWORD_PROPERTY.

What i'm wondering is, by setting these stubs, are these included in the SOAP header already?
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
Hi Jaclyn Tan,
If it's working for you than it's great, but problem that I had is that I couldn't generate stub classes because web service was build in .NET and there was some incompatible types in wsdl. After manualy changing wsdl stub classes was generated but it wasn't working properly since namespaces was incorect and I couldn't set uname&pass. As I recall by setting those properties (Stub.USERNAME_PROPERTY and Stub.PASSWORD_PROPERTY) it does not write authentication in soap header (but you can test it in emulator if you're using WTK , just enable 'network monitor' in emulator preferences end it will catch request/responce for you and than you can inspect how your message looks like).
So I had to find a way to create soap message by hand and I've tried with ksoap but couldn't find a way to write username&pass in header (think there is a way but gave up too soon).
Finnaly I've done it as I describe in previous post.
Guadalupe Ortiz
Ranch Hand

Joined: Sep 12, 2003
Posts: 74
Dear Jaclyn,
Could you please provide me with a simple sample code of how you included info in the SOAP Header with the JSR 172. I am currently trying with KSOAP and it is nightmare. I do not discard either do it Aleksander way, but I would be great to do it a nicest way.
I f you could give some example I would be really grateful.
Thanks a lot,
Guadalupe
Jaclyn Tan
Greenhorn

Joined: Jan 16, 2009
Posts: 6
Hi Aleksandar,
Thanks for the response. I looked at the request and response of these web services and all it has is an empty header tag. As for your suggestion, i don't think my superiors would agree to hardcoding the header information as it would be very risking especially dealing with security authentication. I will keep on looking until i find the solution to this dilemma.

Hi Guadalupe,
I wasn't able to include information in the SOAP header. As what i explained to Aleksandar, i was only able to set the properties of the stubs, using Stub.USERNAME_PROPERTY and Stub.PASSWORD_PROPERTY. But if you mean this example, then i can give you a sample code. I am still trying to figure out how to include information in the SOAP header.
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
@Jaclyn Tan
Just want to give you a hint because you said that you don't want to hardcode authentication header.
Before connecting to web service open form for username & pass, and use that data when creating soap message.
If you are using ksoap or generate stub the same thing happens, you will have message and just need to add username&pass. If you hardcode username&pass in application it doesn't metter is it string or stub or anything else. It can be decompiled.

I mannaged to add authentication in header with ksoap by using envelope.headerOut
but there is major problem with prefix and namespace that I cannot solve, and always get server error.
For envelope.headerOut you can add authentication as an Element[] (org.​kxml2.​kdom.Element)
But again you'll need to hardcode Element[] structure of authentication header.

There is some progress I made by extending ksoap classes and override prefix and namespace creation but then why should I use ksoap if I need to create my own
Jaclyn Tan
Greenhorn

Joined: Jan 16, 2009
Posts: 6
hi aleksandar,

can you help me with the headerOut in kSoap? i was trying it but somehow, my username and password is always a blank. this is what i did:



im using a .net web service which returns the username and password that was passed. but all i get is null. i checked the request dump, the headers were already included. Can you please help me?
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
You can do something like this

but still you'll have problem with prefix since ksoap adds some "strange" prefixes on every node.

In SoapEnvelope.class there is method

that is calling writeHeader and writeBody methods.
You can see prefixes it adds to nodes (i,d,c,v).
Jaclyn Tan
Greenhorn

Joined: Jan 16, 2009
Posts: 6
Hi Aleksandar,

Thank you very much for your quick reply.

Your first suggesstion worked for me. I just replaced that part of my code with what you provided and it worked. I didn't have any problem with the prefixes or whatsoever. Isn't it amazing? And i owe it all to you. Thank you very much!

Now my next step is how to do this using JSR172. Do you have any idea on how to do this?
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
It's great that it works for you
If you could post your code. I would like to see how did you create envelope.

For jsr-172 I don't have much to say.
There is stub generator in wtk that you can use to create message. The other way is http://www.netbeans.org/kb/60/mobility/midp-webservices.html
It should provide basic http authentication but it's not working with soap authentication and sometimes there is a problem with generating stubs from wsdl created with .net
Jaclyn Tan
Greenhorn

Joined: Jan 16, 2009
Posts: 6
This is my .NET Web Service. AuthHeader is a class that contains two strings, username and password.



For the kSOAP call, I just included the function executeWebService(). This is my code.



As for JSR172, I did not create any stubs of the Web Service because I don’t have a WTK with me.

I found this tutorial about JSR172 and how to implement it, and that’s how I did it. If you want to read about it, its title is "J2ME Web Services Specification" from Sun Microsystems. I could not upload it in this post because it does not allow .pdf files.

I’ve used almost all classes that I could in trying to create a web service client using JSR172. I’ve used javax.xml.rpc, javax.xml.namespace.QName and javax.microedition.xml.rpc. The only class I haven’t used so far is the javax.xml.parsers. But I don’t know what’s the use of this in my problem. Is there really no available api that I can use to add these SOAP headers in JSR172? I really have to hardcode the SOAP message like you said in your earlier post?
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
Hi Jaclyn Tan,
Sorry for not posting earlier.

Thanks for your code but I still have problem with prefix when using ksoap, and jsr-172 doesn't have option to set soap header.
It seems that you need to hardcode soap message, just like me.
It's easier when you create web service, but if you need to make client and have only wsdl I can give you a good tip.
Use SoapUI. It will create all required classes and test web service, so you can inspect how your soap message should look like in both row and xml format.
Jaclyn Tan
Greenhorn

Joined: Jan 16, 2009
Posts: 6
Hi Aleksandar,

Thanks for the advice.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Including information in the SOAP header in a web service invocation.
 
Similar Threads
SOAP header blocks: Are they specific to the binding?
Consume dot net webservice
This weeks book giveaway
doubt in web service security
web service tokens