Android Question Consuming a WCF webservice - Bad request and Internal Server Error!

bubbu

New Member
Licensed User
Longtime User
Hello to all,

First of all, because this is my first post, i would like to praise the development team of B4A, because it's one of the best products i've used in the recent times.
Now, speaking of work...

I have the necessity to consume a WCF webservice. I've read almost all posts in the forum talking about webservices and i've managed to put out some code that, i thought, would work.

B4X:
Sub mnuRefresca_Click()

    Dim htJob As HttpJob
    Dim strXML As String = File.ReadString(File.DirAssets, "ObtemEmpresas.xml")
    'Dim strXML As String = File.ReadString(File.DirAssets, "Autenticacao.xml")
    Log(strXML)
    htJob.Initialize("htJob", Me)
    htJob.PostString("http://www.*****.pt:82/ws/Autenticacao/AutenticacaoCliente.svc?wsdl", strXML)
    htJob.GetRequest.SetContentType("text/xml; charset=utf-8")
End Sub

Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        'print the result to the logs
        Log(Job.GetString2("UTF8"))
        ToastMessageShow(Job.GetString2("UTF8"),True)
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

I've uploaded a simple test project with those files and this code.
I have two problems:

1 - using obtemempresas.xml when program enters in JobDone, i get always "Bad Request" error. I've created a Visual Studio application and a way to consume the webservice, having Charles Web Debugging Proxy to get the soap messages, so the soap is an exact copy of that (the same thing with Autenticacao.xml)

2-using autenticacao.xml i also get an error, but this time is "Internal Server Error".

Since in here there are much more experienced eyes than mine, i would like to know if anyone can give a hand, that would be very appreciated!!
Keep up with the good work!!
 
Last edited:

bubbu

New Member
Licensed User
Longtime User
I found the solution:
B4X:
Sub mnuRefresca_Click()

    Dim htJob As HttpJob
    Dim strXML As String = File.ReadString(File.DirAssets, "Autenticacao.xml")
    Log(strXML)
    htJob.Initialize("htJob", Me)
    htJob.PostString("http://www.*****.pt:82/ws/Autenticacao/AutenticacaoCliente.svc", strXML)
    'Added this line below
    htJob.GetRequest.SetHeader("SOAPAction", """http://Server/SoapAction""")
    htJob.GetRequest.SetContentType("text/xml; charset=utf-8")
End Sub

Now things are running smoothly! Thanks!
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi,

I know this is an old post but, I have a similar problem connecting to a WCF. I have confirmed the WCF works since I've tested it with the VS2010 tool. However, when I copy over the XML to B4A and run the app, I get a "Not Found" error. What I don't know from this error is what is not found. Any ideas?

Thanks
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
B4X:
LogCat connected to: CB5A1XFMR3
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
JobName = Job2, Success = false
Error: Not Found

Not sure what you can make from this. My first version was just using a simple Windows WS, which works perfectly, so I know it's not the VB code. I suspect it's the XML which I'm passing. I think there's tags in there which are either invalid or I haven't correctly formatted.

For your reference here's the two different types of xml that I have.

B4X:
<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body><Sum xmlns=""http://tempuri.org/""><a>1</a><b>1</b></Sum></soap:Body></soap:Envelope>

The above works 100% :)

B4X:
<?xml version=""1.0"" encoding=""utf-8""?><s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Header><Action s:mustUnderstand=""1"" xmlns=""http://schemas.microsoft.com/ws/2005/05/addressing/none"">http://tempuri.org/IService1/AuthenticateDevice</Action></s:Header><s:Body><AuthenticateDevice xmlns=""http://tempuri.org/""><DID>unknown</DID></AuthenticateDevice></s:Body></s:Envelope>

This doesn't work. :(

In both instances I got the XML from the test pages that was created by IIS.

Thanks.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi Erel,

I have tried everything and I cannot get it to work. The closest I get is "Not Found". Anything else I try gets me a host of errors. I know I asked this previously but, what exactly is the "not found" referring to? Is it that it can't see the WCF or is it the method within the WCF?

Thanks
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi Erel,

I have tried everything and I cannot get it to work. The closest I get is "Not Found". Anything else I try gets me a host of errors. I know I asked this previously but, what exactly is the "not found" referring to? Is it that it can't see the WCF or is it the method within the WCF?

Thanks

Hi Indy,

I've faced your problem; try turning off the windows (or other?) firewall. If this works (as did for me), then you can open the corresponding ports.
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi,

I'm getting the internal server error (500) message, and I can't solve it. I've added headers, the SoapUI works OK, but I can't figure it out. This is the SOAP, according to SoapUI, with a test-value (15):

B4X:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetData>
         <!--Optional:-->
         <tem:value>15</tem:value>
      </tem:GetData>
   </soapenv:Body>
</soapenv:Envelope>

...and this is my template:

B4X:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetData>
         <!--Optional:-->
         <tem:value>$VALOR$</tem:value>
      </tem:GetData>
   </soapenv:Body>
</soapenv:Envelope>

...and this is my code:
B4X:
Sub Activity_Create(FirstTime As Boolean)

   template = File.ReadString(File.DirAssets, "mytemp.xml")
   template=template.Replace("$VALOR$","15")
     
   Dim job As HttpJob
   job.Initialize("Job", Me)
   job.PostString("http://192.168.1.113:5674/svcSecret.svc", template)
   job.GetRequest.SetHeader("SOAPAction", """http://tempuri.org/iSvcSecret/GetData""")
   job.GetRequest.SetContentType("text/xml; charset=utf-8")
  
End Sub



...and this is my error:

B4X:
Installing file.
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Error en el servidor remoto: (500) Error interno del servidor.
JobName = Job, Success = false
Error: Internal Server Error


notice that if I paste the address (http://192.168.1.113:5674/svcSecret.svc) on the web browser, it works OK (shows the service example page)

Thanks for your help!
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi,

Trying to configuring fiddler I've realized that the issue has to be with the proxy thing (since visual studio inbuilt IIS for testing does not suport incoming connectios from "outside" localhost). The proxy I'm using works well with an asmx web service but I can't make it work with a IIS hosted WCF.

I've switched to a non-IIS-hosted WCF (a windows service one), with basichttpbinding, and it works.

Thanks for your help anyway!
 
Upvote 0
Top