Page 1 of 1

[Solved]XML over Sockets

Posted: Mon Sep 29, 2014 3:31 pm
by zaxos
I'v been trying to send some xml commands through sockets but i had no success so far.. her's the xml code

Code: Select all

<SL:root xmlns:SL="Smartlaunch"> 
<Command Name="SmartlaunchVersion" /> 
</SL:root>
and hers the link of the program API i'm trying to work with : http://smartlaunch.blob.core.windows.ne ... 140710.pdf
any help would be apreciated since i'm desperate with thisone

Re: XML over Sockets

Posted: Tue Sep 30, 2014 4:21 pm
by bangkok
A thread has been created with the same subject. :)

http://forums.livecode.com/viewtopic.php?f=7&t=20161

Re: XML over Sockets

Posted: Wed Oct 01, 2014 11:28 am
by zaxos
Thank you for your reply bangkok, i'v seen that thread and it offers no resolution to my problem. It seems that the problem is with Livecode socket system since i try'd sending xml commands with PHP,VB,C# even socket testers and it worked as it should... From my understanding i'd need to learn how sockets, HTTP over sockets, XML commands, HEADERS, work and then adjuct that with Livecode . But well the reason i choose Livecode is because its simple... Anyway the resolution for me right now was making a socket script in VB then i send the commands from Livecode to the VB script and from there to the server, then the scripts writes the result at a txt and so on...
I guess i'l have to wait until Livecode team makes my life better for this :) .

Re: XML over Sockets

Posted: Wed Oct 01, 2014 2:23 pm
by bangkok
Hum... Sockets work fine with LiveCode. And sending "XML" commands through socket connexion is not a problem, since XML format is just regular strings.

But in your case you speak about a HTTP server,right ?

Then you should perhaps look at POST command, along with header settings, or revXMLRPC_CreateRequest command.

In any case, you should give us more details about what you try to achieve, which server/app you try to communicate with.

Re: XML over Sockets

Posted: Wed Oct 01, 2014 2:35 pm
by bangkok
Ok, sorry I didn't read the API part of your first post.

So basically you want to connect to a "web service" that uses XML format, through HTTP connexions.

So from my understanding and my experiences with web services (XML and JSON) :

-you need to compose your XML query

Code: Select all

put "<SL:root xmlns:SL="&quote&"Smartlaunch"&quote&">" into tQuery
put "<Command Name="&quote&"SmartlaunchVersion"&quote&"/>" after tQuery
put "</SL:root>" after tQuery
-then set a header

Code: Select all

 libURLSetCustomHTTPHeaders
or

Code: Select all

   set the httpheaders to xxxx
-and then just do a POST (or a GET) to the HTTP server with the query

Something like :

Code: Select all

   libURLSetCustomHTTPHeaders field "Header"
   post tQuery to url "http://vsrvx3:18980/adxwsvc/services/CAdxSubProgramXml"
Header can be typically

Code: Select all

Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
Content-Length: 1871
Host: vsrvx3:18980
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
[the important part is the length]


Or

Code: Select all

Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=UTF-8
Cookie: session_id=69b6282312f2fe1f88cd403ed075db8f52c4ea57
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Tip : in order to find the proper header, install Fiddler2 (HTTP debugging proxy server application).

Then launch your VB script (using the proxy created by Fiddler)... Fiddler will catch and display all data going out. After that, easy to "mimic" the header with LiveCode.

You find will more infos here :

http://smartlaunch.helpserve.com/Knowle ... umentation

Re: XML over Sockets

Posted: Wed Oct 01, 2014 5:24 pm
by zaxos
Thank you very much for your quick reply :), problem is that i have an older version of Smartlaunch wich means i cant use RESTFUL api, if i understand this well with restful you can just post and you get an answer but with their old api you just send the command... Now for the fiddler part i have actually tried to capture the headers and even tho my socket connection is successful fiddler shows nothing.
Her's what i'v tried so far :

Code: Select all

on mouseUp
open socket to theSocket -- theSocket = my socket
write XMLCommand to socket theSocket -- XMLCommand = the command var
read from socket theSocket until EOF
put it
end mouseUp
now the strange thing is that i always succesfuly connect to the socket and i seem to be able to send commands i just get no reply and the commands ofc dont work.
One simple question: http://smartlaunch.blob.core.windows.ne ... 140710.pdf , is this using the same method as RESTFUL? do i have to use post with this version too? If not, do i have to change the headers when sending data through sockets too? and why isnt fiddler capturing the connection?
Again my friend thank you for your time, you make me feel more optimistic. :)

Re: XML over Sockets

Posted: Wed Oct 01, 2014 7:24 pm
by bangkok
Fiddler : don't forget that you need to use its proxy.

Therefore to make test with a LiveCode script you need to add into your script :

Code: Select all

set the HTTPProxy to "127.0.0.1:8888"


OK. So we're back at the start : you need socket connexion, and not to a HTTP server. And then the commands are just plain XML strings.

So I would say that you need to debug your socket script. It should work.

One pitfall I can see : are you sure that the server finishes its answers by EOF ? If not then your script wont work. And what about a special end character for the data you send to the server ?

This is why it's important to manage to use Fiddler with your VB script, the one that actually manages to connect to the server and sends commands and receives proper answer. You will be able to analyse exactly what's going on between the server and the client.

Put this script into a button, and try it.

Code: Select all

on mouseUp
   disable me
    
   put "192.9.202.135:8192" into tSocket  --- change IP and port number
   
   open socket to tSocket with message connected

   If the Result <> "" then
      answer warning "Error : "&the Result
      enable me
      exit to top
   end if
   
end mouseUp

on connected theSocket
   write fld "xml"& return to socket theSocket  --- put into a field your whole XML command, beware of the last char : return or something else ? What the server is waiting for ?
   wait 5 ticks
   read from socket theSocket  for 4 chars with message "login1"  ---it should read the first 4 chars of the server's answer and go to "login1" as a callback message.
end connected

on login1 theIP tMessage
   close socket theIP
   answer   tMessage --- display the 4 characters received (if any)
   enable me
end login1

on socketError theID, tDetail
   answer warning "ERROR "&tDetail
   close socket theID
end socketError

Re: XML over Sockets

Posted: Wed Oct 01, 2014 7:43 pm
by zaxos
OMG, YOU ARE MY GOD ! I LOVE U !! for some reason the ending word was {end} after some trial and fail FINALY IT WORKED!! Man Realy Realy thank you very much i'v been trying to fix that for like a month !
Since i'm not sure what the resolution was with this one besides the ending word if anyone in future has the same problem just look at bangkok's answer and you'l figure it out... :D :D :D

Re: [Solved]XML over Sockets

Posted: Wed Oct 01, 2014 8:07 pm
by zaxos
So, new problem, server requires authentication, "ExternalAuthorization=UserName^Password" for the first time this command is working just find, i get a message back saying "Authorized" i changed the script to keep the connection open but the any command i send after returns no answer for some reason... Any suggestions are welcomed :)

Re: [Solved]XML over Sockets

Posted: Thu Oct 02, 2014 9:08 am
by zaxos
After a closer look at this it turned out that the answer to my problem was simplier than i thought and yet i'd never found it without bangkok's help. It wasent the {end} tag since i had used this in the past without results, it was the read from socket since i had tryed until EOF, until empty, until linefeed, until return, for 10 lines, but never thought of trying for xx chars... thats what did the trick for me, the only catch here is that if the response is 500 chars and you tell livecode to read from socket for 600 chars e.x it wont work... the number of chars has to be less or equal to the response... i guess i'l have to work on those headers to get the length of the response to read it.