URLEncode (Socket function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
Line 41: Line 41:
</ul>
</ul>


==Example==
==Examples==
 
In this example, <var>URLEncode</var> creates <code>%-hex-hex</code> encoding
In this example, <var>URLEncode</var> creates <code>%-hex-hex</code> encoding
for this browser request to an HTTP server in the HTTP Get format:
for this browser request to an HTTP server in the HTTP Get format:

Revision as of 19:20, 11 June 2012

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

%string 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 (socket) 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&#58;//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:

    . ( ) ! $ * - _

Examples

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 (/).

See also