Test the rest server in Delphi Xe. One of the reversestring server functions is to flip an incoming string. The datasnap call is:
Procedure tform1.button1click (Sender: tobject); <br/> var <br/> VC: tdbxcommand; <br/> P: tdbxparameter; <br/> begin <br/> sqlconnection1.params. values ['communicationprotocol']: = 'HTTP '; <br/> sqlconnection1.params. values ['hostname']: = eddsserver. text; <br/> sqlconnection1.params. values ['Port']: = eddsport. text; <br/> sqlconnection1.params. values ['dsauthenticationuser']: = edusername. text; <br/> sqlconnection1.params. values ['dsauthenticationpassword']: = edpassowrd. text; <br/> sqlconnection1.open; </P> <p> VC: = sqlconnection1.dbxconnection. createcommand; <br/> VC. commandtype: = tdbxcommandtypes. dsservermethod; <br/> VC. text: = 'tservermethods1. reversestring '; <br/> VC. prepare; <br/> VC. parameters [0]. value. setstring (edit2.text); <br/> VC. executeupdate; <br/> edit1.text: = VC. parameters [1]. value. asstring; <br/> VC. free; <br/> end;
However, when calling http get, you must pay attention to the parameter encoding:
Procedure tform1.button2click (Sender: tobject); <br/> var <br/> VMS: tmemorystream; <br/> B: tbytes; <br/> J: tjsonobject; <br/> S: string; <br/> vurl: string; <br/> begin <br/> VMS: = tmemorystream. create; <br/> try <br/> vurl: = 'HTTP: // '+ eddsserver. text + ':' + eddsport. text + '/datasnap/rest/tservermethods1/reversestring/'; <br/> vurl: = vurl + paramsencode (edit2.text); <br/> idhttp1.get (vurl, VMS ); <br/> setlength (B, VMS. size); <br/> VMS. position: = 0; <br/> VMS. read (B [0], VMS. size); <br/> J: = tjsonobject. create; <br/> J. parse (B, 0); <br/> edit1.text: = tjsonarray (J. get (0 ). jsonvalue ). get (0 ). value; <br/> J. free; <br/> finally <br/> VMS. free; <br/> end; <br/>
When encoding parameters, there are two ready-made functions:
1. tiduri. paramsencode of iduri. Pas
This function defines the characters to be escaped as: unsafechars: tidunicodestring = '* <>#% "{}|/^ []''; {do not localize}
But this is not enough. These two characters do not include :/?, If the parameters contain these characters, the result is incorrect or the call fails. Modify this function to add the two characters.
2. httpencode of httpapp. Pas
First, the parameter of this function is ansistring. Second, the space is converted into a plus sign, which cannot meet the requirements.