Get (HttpRequest function): Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 23: | Line 23: | ||
<td>This optional ([[Notation conventions for methods#Named parameters|name required]]) argument invokes an SSL certificate request from the client at the time of the method call. | <td>This optional ([[Notation conventions for methods#Named parameters|name required]]) argument invokes an SSL certificate request from the client at the time of the method call. | ||
The argument value (<var class="term">certificateChecker</var>) must be a function, presumably [[Local and Common entities#Defining and invoking a local method|local]], with this [[Method variables#Method template in method variable declaration|method template]]: | The argument value (<var class="term">certificateChecker</var>) must be a function, presumably [[Local and Common entities#Defining and invoking a local method|local]], with this [[Method variables#Method template in method variable declaration|method template]]: | ||
<p class="syntax"><span class="literal"> | <p class="syntax"><span class="literal">Function (String):</span><span class="term">methname</span> <span class="literal">Is Boolean</span> </p> | ||
When a connection is established, the passed function is called with the certificate (copied to a <var>Longstring</var>) as its method object. | When a connection is established, the passed function is called with the certificate (copied to a <var>Longstring</var>) as its method object. | ||
Line 74: | Line 74: | ||
<li>For HTTP Version 1.1 mode only, the HTTP request header "connection: close" is automatically sent on GETs and POSTs to ensure that the server closes the connection with the client when the HTTP request completes. | <li>For HTTP Version 1.1 mode only, the HTTP request header "connection: close" is automatically sent on GETs and POSTs to ensure that the server closes the connection with the client when the HTTP request completes. | ||
The <var class="product">HTTP Helper</var> does '''not''' support persistent connections. | The <var class="product">HTTP Helper</var> does '''not''' support persistent connections. | ||
<li>If the ports associated with the sockets in the HTTP request are not using SSL, and the <var>CertificateCheck</var> argument is specified, the <var class="term">certificateChecker</var> function does not get called. | |||
<li>For more information about the HTTP GET request format and about the HTTP protocol, refer to http://www.w3.org/Protocols/rfc2616/rfc2616.html. | <li>For more information about the HTTP GET request format and about the HTTP protocol, refer to http://www.w3.org/Protocols/rfc2616/rfc2616.html. | ||
</ul> | </ul> | ||
Line 117: | Line 118: | ||
%resp = %req:get(certificateCheck=foo, checkKeepAliveCertificate=true) | %resp = %req:get(certificateCheck=foo, checkKeepAliveCertificate=true) | ||
... </p> | ... </p> | ||
This variation of the preceding example shows that the <var class="term">certificateChecker</var> function may contain code to throw an exception: | |||
<p class="code">local function (string):foo is boolean throws badCert | |||
printText {~=%this:length} | |||
printText {%this:stringToHex} | |||
printText {%this:asciiToEbcdic(characterEncode=true)} | |||
return true | |||
%bad is object badCert | |||
if ... | |||
throw %bad | |||
end if | |||
end function | |||
... | |||
%req is object httpRequest | |||
%resp is object httpResponse | |||
... | |||
try %resp = %req:get(certificateCheck=foo, checkKeepAliveCertificate=true) | |||
catch badCert | |||
printText Caught a bad certificate | |||
stop | |||
end try | |||
... </p> | |||
==See also== | ==See also== | ||
<ul> | <ul> |
Revision as of 01:13, 16 November 2011
Send an HTTP GET request to the server (HttpRequest class)
The Get method sends an HTTP request to an HTTP server in the HTTP GET format: the request syntax sent to the server is the same as if a METHOD="GET"
HTML form was submitted from a web browser, or as if a URL was simply clicked.
Syntax
%httpResponse = httpRequest:Get[( [[Port=] string], [[Cancel=] number], - [CertificateCheck= certificateChecker], - [CheckKeepAliveCertificate= boolean])]
Syntax terms
%httpResponse | A reference to the HTTPResponse object instantiated and returned by this Get method. |
---|---|
httpRequest | The previously defined and instantiated HttpRequest object that contains the request (its methods were used to create the request). |
Port | A string expression that identifies a previously defined CLSOCK port. The string default is the MASTER port, if any is defined. However, if you omit string and no MASTER port is defined, the request is canceled.
This is an optional and (as of Sirius Mods version 7.2) name allowed parameter. If you specify a value, you may (but don't have to) specify the parameter name, Port (case not important). |
Cancel | A numeric expression that controls whether the request is cancelled if a communication/socket error occurs. If zero, the request is not cancelled on these errors. If non-zero, the request is cancelled (though a message is still issued). If not specified, it defaults to 1 (cancel). See "Usage notes," below, for further discussion of error handling.
This is an optional and (as of Sirius Mods version 7.2) name allowed parameter. If you specify a value, you may (but don't have to) specify the parameter name, Cancel (case not important) if you wish. |
CertificateCheck | This optional (name required) argument invokes an SSL certificate request from the client at the time of the method call.
The argument value (certificateChecker) must be a function, presumably local, with this method template: Function (String):methname Is Boolean When a connection is established, the passed function is called with the certificate (copied to a Longstring) as its method object.
For a CertificateCheck function example, see "Examples". |
CheckKeepAliveCertificate | This optional (name required) argument invokes |
Usage notes
- Two classes of non-application errors can occur when Get is invoked:
- The HTTP request completed, but the server indicated a failure of the request (an HTTP return code of 400 or greater). An example of this is the "404 not found" message issued by a web server when a non-existent document is referenced. In this class of error, there was no TCP/IP communication error. For these errors, the request is never cancelled, and an HttpResponse object is always returned. The application handles these errors by checking the HTTPResponse Code, StatusLine, Message, and Success methods.
- While attempting the HTTP request, a socket/communication error occurred. Examples of such errors include failure to connect to the server host, and communication failure on a TCP/IP send or receive.
For this class of errors, no instance of HTTPResponse is created, and a Null object is returned. In addition, $STATUS is set to 1, and $STATUSD is set to one of these:
- A negative return code that indicates an error in connecting to the target HTTP server. These codes are shown below in "Get method return values".
- A positive return code indicating a communications error after the connection was made.
The request may or may not be cancelled based on the setting of the Cancel parameter.
Some of these errors are relatively benign (for example, inability to connect because remote partner down, timeout value exceeded), so they produce information-only messages, and they are treated as non-counting. These errors can still be detected and handled by the application (by checking for a Null result object, checking $STATUS and $STATUSD, or both).
Note: The distinctions between some of the return codes might or might not be meaningful, depending on the situation. For example, network problems can cause a 101 return code (indicating a timeout) or a 102 (indicating a connection closed). Connecting to a server that is using a non-HTTP protocol can result in a 100 (invalid format data received from the server) or a 101 or 102, depending on the protocol used by the server to which the request is sent: a request sent to a telnet port usually results in a timeout (return code 101), while a request sent to an SSL port without using
https
to indicate SSL usually results in the target server breaking the connection (return code 102).In general, it is risky to take action based on the distinction between the various non-zero error codes returned by this method.
For more information about an error, you may want to issue a JANUS TRACE command with a value of 8 or 15 for the CLSOCK port that is handling your HTTP request.
- Under Sirius Mods versions before 7.1, certain "post-oriented" actions were not allowed with an HTTP GET, and resulted in request cancellation if detected by Get function error checking:
- An AddXml call added Post data to the request.
- An AddLongstring call added Post data to the request.
- The MultiPartFormEncoding property set to
True
enabled multipart form encoding.
Under Sirius Mods 7.1 and later, these methods are allowed before a Get method call. While unusual, passing content in a Get method is not strictly forbidden by the HTTP specification.
- For HTTP Version 1.1 mode only, the HTTP request header "connection: close" is automatically sent on GETs and POSTs to ensure that the server closes the connection with the client when the HTTP request completes. The HTTP Helper does not support persistent connections.
- If the ports associated with the sockets in the HTTP request are not using SSL, and the CertificateCheck argument is specified, the certificateChecker function does not get called.
- For more information about the HTTP GET request format and about the HTTP protocol, refer to http://www.w3.org/Protocols/rfc2616/rfc2616.html.
Get method return values
Code | Meaning |
---|---|
0 | Communications was successful. |
100 | The format of the response from the server was invalid. |
101 | A Get operation exceeded its timeout value. |
102 | The connection was closed before the complete response was received. |
-100 | No free socket numbers for user. |
-101 | Remote host name or IP address missing or mismatch with CLSOCK port definition. |
-102 | Remote port number missing or mismatch with CLSOCK port definition. |
-103 | CLSOCK port not defined or not started. |
-104 | All ports on specified CLSOCK port are busy. |
-105 | Insufficient virtual storage. |
-106 | Maximum connections exceeded. |
-107 | Couldn't resolve remote host. |
-108 | Remote port not responding. |
-109 | Already have SOCKPMAX sockets open on this CLSOCK port. |
-110 | SSL/NOSSL setting mismatch. |
-111 | SSL handshake error. |
-112 | Access to CLSOCK port not enabled by ALLOW rule. |
Examples
In this code fragment, the CertificateCheck argument of Get invokes a local method that displays some
information about the client socket's SSL certificate. The local method returns a True
enumeration value to the Get, and the request continues:
local function (string):foo is boolean printText {~=%this:length} printText {%this:stringToHex} printText {%this:asciiToEbcdic(characterEncode=true)} return true end function ... %req is object httpRequest %resp is object httpResponse ... %resp = %req:get(certificateCheck=foo, checkKeepAliveCertificate=true) ...
This variation of the preceding example shows that the certificateChecker function may contain code to throw an exception:
local function (string):foo is boolean throws badCert printText {~=%this:length} printText {%this:stringToHex} printText {%this:asciiToEbcdic(characterEncode=true)} return true %bad is object badCert if ... throw %bad end if end function ... %req is object httpRequest %resp is object httpResponse ... try %resp = %req:get(certificateCheck=foo, checkKeepAliveCertificate=true) catch badCert printText Caught a bad certificate stop end try ...
See also
- For more information about $STATUS and $STATUSD, see the Model 204 User Language Manual.
- You can also send an HTTP GET using the Send method.