URLEncode (Socket function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Created page with " <span class="pageSubtitle"><section begin=dpl_desc/><section end=dpl_desc/></span> URLEncode function <p> <var>URLEncode</var> is a member of the <va...")
 
mNo edit summary
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Template:Socket:URLEncode subtitle}}
<span class="pageSubtitle"><section begin=dpl_desc/><section end=dpl_desc/></span>
[[Category:Socket methods|URLEncode function]]
<p>
<var>URLEncode</var> is a member of the <var>[[Socket class|Socket]]</var> class.
</p>


This method uses the URL "% hex hex" escape format to
This method uses the URL "% hex hex" escape format to
Line 10: 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==
<p class="syntax">%lstr = %socko:URLEncode(string)
{{Template:Socket:URLEncode syntax}}
</p>
 
===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>
<tr><th>%socko
<tr><th>socket
</th><td>A variable or an expression that is a reference to a <var>Socket</var> object.
</th><td>A variable or an expression that is a reference to a <var>Socket</var> object.
</td></tr>
</td></tr>
Line 28: 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 37: Line 33:
must be indicated to <var>URLEncode</var> via the <var>Socket</var> object.
must be indicated to <var>URLEncode</var> via the <var>Socket</var> object.
<li><var>URLEncode</var> is particularly useful for constructing URLs, especially
<li><var>URLEncode</var> is particularly useful for constructing URLs, especially
when composing the query string portion of a URL (<code>http://host/path?query</code>).
when composing the query string portion of a URL (<code>http&amp;#58;//host/path?query</code>).
<var>URLEncode</var> is also useful for composing form data for a post request over an HTTP
<var>URLEncode</var> is also useful for composing form data for a post request over an HTTP
connection.
connection.
<li>The non-alphanumeric characters that <var>[[$Sock_Url_Encode]]</var> does not encode are these seven:
<li>The non-alphanumeric characters that <var>$Sock_Url_Encode</var> does not encode are these seven:
<p class="code">. ( ) ! $ * - _
<p class="code">. ( ) ! $ * - _
</p>
</p>
</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 55: 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 67: 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.
{{Template:Socket:URLEncode footer}}
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>

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