Recent

Author Topic: LAMW: jHttpClient - how to make soap call? (http POST) - little workaround  (Read 1460 times)

Bazic

  • Newbie
  • Posts: 4
First of all: to all LAMW group members: Your work is GREAT !!!!

I try to use android as a client of my soap server app as I used to on wince target.
It seems on android target this is very, very different.

I coud not achieve jHttpClient making calls to soap server. (Maybe I was completely wrong in a way doing that - if so - please lighten me up)
I tried to do raw POST, but xml part was always mangled. Server replied with an error.

I looked under the hood :) and I figured out workaround.

Below is little example code:

Code: Pascal  [Select]
  1.   jHttpClient1.ClearNameValueData;
  2.   jHttpClient1.AddClientHeader('Content-Type','text/xml; charset=utf-8');
  3.   jHttpClient1.AddClientHeader('SOAPAction', '"urn:OSWIntf-IOSW#Wersja"');
  4.  
  5.   content:='<?xml version="1.0" encoding="UTF-8"?>'#13#10
  6.   +'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:OSWIntf-IOSW" '
  7.   +'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  8.   +'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  9.   +'<SOAP-ENV:Body><ns1:Wersja/></SOAP-ENV:Body></SOAP-ENV:Envelope>';
  10.  
  11.   jHttpClient1.AddNameValueData('BODY',content);
  12.  
  13.   jHttpClient1.PostNameValueDataAsync('http://192.168.1.3/soap/IOSW');
  14.  


"Wersja" is function name I try to call at the server side.

Of course in my POST instead of expected raw xml data was: BODY=<?xml version="1.0"....   but encoded as: BODY==3c=3fxml version=3d1.0=3d........


When I changed some lines of getPostDataString function in a file jHttpClient.java placed in: C:\lamw\lazandroidmodulewizard\java\lamwdesigner

(starting at line # 163)

from:

Code: Pascal  [Select]
  1.  
  2.               result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  3.               result.append("=");
  4.               result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  5.  

to:

Code: Pascal  [Select]
  1.        if (entry.getKey().equals("BODY")==true) {              
  2.          result.append(entry.getValue());
  3.         } else {
  4.               result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  5.               result.append("=");
  6.               result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  7.         }
  8.  

then "BODY" key became special. If it is first key then its name will not be used and its value will not be url encoded and xml structure will be fine for soap server.

I have two questions here: Is there any better way to consume soap services wiyh LAMW? Is there possibility to (somehow) use WST package?


jmpessoa

  • Hero Member
  • *****
  • Posts: 1316
Re: LAMW: jHttpClient - how to make soap call? (http POST) - little workaround
« Reply #1 on: September 22, 2017, 11:20:35 pm »
(1)I think the issue is how  android api handle:
"jHttpClient1.AddNameValueData('BODY',content);"
when  we have others "=" in "content"...   I will try some solution!
 
(2)To use FPC "FCL" core, often we need to copy the source code to your project "jni"  folder ....

Example: To use  "fphttpclient" copy "fphttpclient.pp",  "httpdefs.pp" and "httpprotocol.pp" to your project "jni" folder
« Last Edit: September 23, 2017, 03:00:54 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

jmpessoa

  • Hero Member
  • *****
  • Posts: 1316
Ok.

I improved jHttpClient!

Some general solution:

Code: Pascal  [Select]
  1. jHttpClient1.SetUnvaluedNameData('BODY'); //disregard nameData 'BODY'
  2.  
  3. jHttpClient1.SetEncodeValueData(False);  //not encode ValueData
  4.  
  5. jHttpClient1.AddNameValueData('BODY',content);
  6.  
  7. jHttpClient1.PostNameValueDataAsync('http://192.168.1.3/soap/IOSW');
  8.  
« Last Edit: March 25, 2019, 08:06:12 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Robert Gilland

  • Full Member
  • ***
  • Posts: 118
Just a quick question why were not these changes available in Lazarus -> Online Package Manager - LAMW --> Update?