URLEncode (Socket function): Difference between revisions
mNo edit summary |
m (1 revision) |
(No difference)
|
Revision as of 01:23, 15 November 2011
Use escape sequences to encode special chars (Socket class)
This method uses the URL "% hex hex" escape format to
encode the special characters in the string you specify.
The URLEncode function has an effect similar to its equivalent $function, $Sock_Url_Encode.
Syntax
%string = socket:URLEncode( string)
Syntax terms
%lstr | Longstring that is to contain the modified, encoded version of the input string string. |
---|---|
socket | A variable or an expression that is a reference to a Socket object. |
string | The string whose reserved characters are to be encoded using the "% hex hex" format. If this string is omitted, a null string is returned. |
Usage notes
- The Socket method object (%socko) has associated with it the translation table used for the encoding of the special characters (see XTAB). This translation table is necessary because special characters are converted to the hexadecimal representations of their ASCII values, even though the input and output strings are both in EBCDIC. Data that must be encoded as hexadecimal must be translated to ASCII first, which means that the appropriate ASCII-to-EBCDIC translation table must be indicated to URLEncode via the Socket object.
- URLEncode is particularly useful for constructing URLs, especially
when composing the query string portion of a URL (
http://host/path?query
). URLEncode is also useful for composing form data for a post request over an HTTP connection. - The non-alphanumeric characters that $Sock_Url_Encode does not encode are these seven:
. ( ) ! $ * - _
Example
In this example, URLEncode creates %-hex-hex
encoding
for this browser request to an HTTP server in the HTTP Get format:
... %rc = %sock:Send('GET /') %rc = %sock:Send(%url) %rc = %sock:Send('?info=') %rc = %sock:Send(%sock:URLEncode(%info)) %rc = %sock:SendWithLineEnd(' HTTP/1.0') ...
For example, if %info
contains:
10 + 3.14 * R*R*R * 2/3
The following value is sent to the web server after the info=
:
10+%2B+3.14+*+R*R*R+*+2%2F3
+
signifies a blank, %
introduces a hex encoding,
2B
is the ASCII code for a plus sign (+), and 2f
is the ASCII code for a
slash (/).
Sample Code
Here is a code fragment that fetches the Sirius home page using a Socket object. You may want to contrast this with the $function sample in "Simple echo of HTTP/HTML".
%socko object Socket %socko = New('HTTPCLIENT', 'www.sirius-software.com', 80) If %socko Is Null Then Print 'Error:' And $STATUSD Stop End If %socko:SendWithLineEnd('GET / HTTP/1.0') %socko:SendWithLineEnd( ) %socko:Set('PRSTOK','AMBIG|0D0A|0A|0D') Repeat While ( %socko:ReceiveAndParse(%s) ) Print %s End Repeat Print %(Socket):Num('RESET') Print %(Socket):ErrInfo('CODE') %socko:Close