URLEncode (Socket function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
mNo edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 5: Line 5:


The <var>URLEncode</var> function has an effect similar to
The <var>URLEncode</var> function has an effect similar to
its equivalent $function, <var>[[$Sock_Url_Encode]]</var>.
its equivalent $function, <var>[[$Sock_URL_Encode]]</var>.


==Syntax==
==Syntax==
Line 12: Line 12:
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%lstr
<tr><th>%string
</th><td><var>Longstring</var> that is to contain the modified, encoded version of the input string <var class="term">string</var>.
</th><td><var>Longstring</var> that is to contain the modified, encoded version of the input string <var class="term">string</var>.
</td></tr>
</td></tr>
Line 24: Line 24:
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>The <var>Socket</var> method object (<var class="term">%socko</var>) has associated with it
<li>The <var>Socket</var> method object (<var class="term">socket</var>) has associated with it
the translation table used for the encoding of the special characters (see <var>[[XTAB]]</var>).
the translation table used for the encoding of the special characters (see <var>[[XTAB]]</var>).
This translation table is necessary because special characters are converted
This translation table is necessary because special characters are converted
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:
<p class="code"> ...
<p class="code">...
%rc = %sock:Send('GET /')
%rc = %sock:Send('GET /')
%rc = %sock:Send(%url)
%rc = %sock:Send(%url)
Line 51: Line 50:
%rc = %sock:Send(%sock:URLEncode(%info))
%rc = %sock:Send(%sock:URLEncode(%info))
%rc = %sock:SendWithLineEnd(' HTTP/1.0')
%rc = %sock:SendWithLineEnd(' HTTP/1.0')
...
...
</p>
</p>
For example, if <code>%info</code> contains:
For example, if <code>%info</code> contains:
Line 63: Line 62:
slash (<tt>/</tt>).
slash (<tt>/</tt>).


==Sample Code==
==See also==
Here is a code fragment that fetches the Sirius home page using a <var>Socket</var> object.
You may want to contrast this with the $function sample in [[Sample Janus Sockets Programs#Simple echo of HTTP/HTML|"Simple echo of HTTP/HTML"]].
<p class="code">%socko object <var>Socket</var>
%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
</p>
 
{{Template:Socket:URLEncode footer}}
{{Template:Socket:URLEncode footer}}

Latest revision as of 18:06, 30 October 2014

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