Request for WCF Service with username and password

jmorales20001

Member
Licensed User
Longtime User
Hello everyone.

I've been trying to request my WCF Service from my b4a App but I couldn't establish connection.

I traced my service and I could get a "template" for the request (I added a xml file in folder assets (mytemp.xml):

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns: s="http://www.w3.org/2003/05/soap-envelope" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDjskkaIkLpaslasAAAA+kPzZSz+2UKCWp7tDBq2JBDnUJSMAM7jsjakaksmKKquqahsh</VsDebuggerCausalityData>
<o:Security s:mustUnderstand="1" xmlns: o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<u:Timestamp u:Id="_0">
<u:Created>2013-03-13T21:45:31.175Z</u:Created>
<u:Expires>2013-03-13T21:50:31.175Z</u:Expires>
</u:Timestamp>
<o:UsernameToken u:Id="uuid-47e0fgcd-e4g4-2e53-8df9-5e564777g112f-1">
<o:Username>myusername</o:Username>
<o:password>mypassword</o:password>
</o:UsernameToken>
</o:Security>
<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://www.mypublicsite.com/HelloWorldService.svc</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/HelloWorldService/SayHelloWorld</Action>
</s:Header>
<s:Body><SayHelloWorld xmlns="http://tempuri.org/"><HelloWorldType xmlns:a="http://schemas.datacontract.org/2004/07/WcfService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:Language>English</a:Language><a:Name>Michael</a:Name></HelloWorldType></SayHelloWorld></s:Body>
</s:Envelope>


After that, I tried to request from my App using this code (to test I didn't change username and password and I used them as in the template):

B4X:
Sub Globals
   Dim template As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layout")
   template = File.ReadString(File.DirAssets, "mytemp.xml")
      
   Dim job As HttpJob
   job.Initialize("Job", Me)
   job.PostString("http://www.mypublicsite.com/HelloWorldService.svc", template)
   job.GetRequest.SetContentType("application/soap+xml; charset=utf-8")
End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Msgbox(Job.GetString(), "")
      parser.Initialize
      
      'Parsing the xml result
      Dim In As InputStream
      In = Job.GetInputStream
      parser.Parse(In, "Parser")
      In.Close
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub

I've been trying different ways to make the request but I get the message "Error: Bad Request". I think the problem is this tag:

<o:UsernameToken u:Id="uuid-47e0fgcd-e4g4-2e53-8df9-5e564777g112f-1">

Because it's different when I make a new request (I've checked debugging in Visual Studio) but I'm not sure what is the correct way to make a request in this scenario.

I hope you can help me.

Thanks in advance.
 

IanMc

Well-Known Member
Licensed User
Longtime User
I guess you've already searched for SOAP on the forums judging by your code.

This guy got it to work: http://www.b4x.com/forum/basic4android-updates-questions/25785-does-b4a-do-soap.html#post149292

I think the key is to use the free Wireshark packet sniffer app to see what is sent and received from something that does work (Visual Studio or a webpage etc.) and compare it to what you're doing that doesn't work.

Also another good tool is SoapUI

Both apps are available for free from SourceForge.net

Showing my age now but does anyone remember when WireShark was called Ethereal ? :D
 
Upvote 0

jmorales20001

Member
Licensed User
Longtime User
Thanks for your answer Ian. I really appreciate it. I'm checking with this tools right now.

I got the XML Request that I posted using "Inspector WCF". This class adds the functionality to output the raw SOAP XML packets sent and received by the WCF service.

At this moment I'm downloading SOAPUI and I've checked another WCF services with my App (with BasicHTTPBinding, -no encryption or security capabilities-) and I had no problems.

My service is WsHtppBinding and I this make me to ask another question: It is supported (WSHttpBinding) by B4U?, I mean, when I see this tag:

<o:UsernameToken u:Id="uuid-47e0fgcd-e4g4-2e53-8df9-5e564777g112f-1">

It makes me think that although in the Request there is encrypted data and I don't know if I can generate this kind of values.

Thank you. I will comment my new results.
 
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
This may or may not help you but often SOAP is way over the top for what you actually need and the much simpler REST protocol can be a lot easier to:

a) get your head around and...

b) actually get to work

:)

Also it is easier to implement over SSL

Have a look at this page:

Learn REST: A Tutorial
 
Upvote 0

jmorales20001

Member
Licensed User
Longtime User
Thanks for your answer.

I saw the SOAPAction tag in some examples but I couldn't find it in my traces (Wireshark or WCF Inspector). Anyway, I'll keep trying and I'll ask you when I have more doubts.
 
Last edited:
Upvote 0
Top