HTTP Helper: Difference between revisions
mNo edit summary |
|||
(22 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
The <var class="product">HTTP Helper</var> is a high level, object oriented interface to | The <var class="product">HTTP Helper</var> is a high level, object oriented interface to | ||
client sockets that permits you to write a <var class="product"> | client sockets that permits you to write a <var class="product">SOUL</var> HTTP client without | ||
knowledge of socket level programming or the format of HTTP requests and responses. | knowledge of socket level programming or the format of HTTP requests and responses. | ||
Line 6: | Line 6: | ||
<var>[[#The HttpRequest class|HttpRequest]]</var> and <var>[[#The HttpResponse class|HttpResponse]]</var>. | <var>[[#The HttpRequest class|HttpRequest]]</var> and <var>[[#The HttpResponse class|HttpResponse]]</var>. | ||
'''Note:''' | <p class="note">'''Note:''' | ||
To use the <var class="product">HTTP Helper</var> objects, you must have licensed <var class="product">Janus Sockets</var> '''and''' <var class="product">[[Janus SOAP]]</var>. | To use the <var class="product">HTTP Helper</var> objects under versions of Model 204 earlier than 7.5, you must have licensed <var class="product">[[Janus TCP/IP Base]]</var>, <var class="product">[[Janus Sockets]]</var>, '''and''' <var class="product">[[Janus SOAP]]</var>. Under version 7.5 and later, | ||
you do not require <var class="product">[[Janus SOAP]]</var>. </p> | |||
<var class="product">Janus SOAP</var> | |||
For information about using <var class="product">SOUL</var> objects, see [[Object oriented programming in SOUL]]. | |||
==Feature summary== | ==Feature summary== | ||
The following capabilities are provided by the <var class="product">HTTP Helper</var>: | The following capabilities are provided by the <var class="product">HTTP Helper</var>: | ||
<ul> | <ul> | ||
<li>Processing GET and POST HTTP requests, including XML support. | <li>Processing GET and POST HTTP requests, including XML support. | ||
<li>Setting form fields and HTTP request headers and posting XML content. | <li>Setting form fields and HTTP request headers and posting XML content. | ||
<li>Sending arbitrary HTTP POST data (using a <var>[[Longstrings|Longstring]]</var>). | <li>Sending arbitrary HTTP POST data (using a <var>[[Longstrings|Longstring]]</var>). | ||
<li>Browser-like handling of file uploads | <li>Browser-like handling of file uploads | ||
using Multipart Form encoding | using Multipart Form encoding | ||
<li>After a request, accessing the HTTP Status line, the response headers, | <li>After a request, accessing the HTTP Status line, the response headers, | ||
and the content. | and the content. | ||
<li>Accessing the content in its raw form and parsed into lines, | <li>Accessing the content in its raw form and parsed into lines, | ||
or deserializing returned XML into an <var>[[XmlDoc API#The XmlDoc class|XmlDoc]]</var> object. | or deserializing returned XML into an <var>[[XmlDoc API#The XmlDoc class|XmlDoc]]</var> object. | ||
<li>Optional automatic handling of redirects that a | <li>Optional automatic handling of redirects that a | ||
Get or Post method call receives from the server | Get or Post method call receives from the server | ||
<li>Using HTTPS, Secure Socket Layer (SSL) HTTP, as well as standard HTTP. | <li>Using HTTPS, Secure Socket Layer (SSL) HTTP, as well as standard HTTP. | ||
<li>Using HTTP Authentication to access password-protected URLs. | <li>Using HTTP Authentication to access password-protected URLs. | ||
<li>Automatic and transparent translation from ASCII to and from EBCDIC. | <li>Automatic and transparent translation from ASCII to and from EBCDIC. | ||
<li>Using proxy servers. | <li>Using proxy servers. | ||
<li>Using HTTP Version 1.1 or 1.0. | |||
Version 1.1 is the default. | <li>Using HTTP Version 1.1 or 1.0. Version 1.1 is the default. | ||
</ul> | </ul> | ||
==Keep-Alive support== | ==Keep-Alive support== | ||
The client port definition parameter <var>[[KEEPALIVE]]</var> | The client port definition parameter <var>[[KEEPALIVE]]</var> | ||
tells <var class="product">Janus Sockets</var> to keep an HTTP connection to a web server open for a certain | tells <var class="product">Janus Sockets</var> to keep an HTTP connection to a web server open for a certain | ||
number of seconds so <var class="product">Janus Sockets</var> can send another request on the same connection. | number of seconds so <var class="product">Janus Sockets</var> can send another request on the same connection. | ||
Line 77: | Line 88: | ||
There may be a timing problem with a keep-alive connection if a request | There may be a timing problem with a keep-alive connection if a request | ||
tries to use a keep-alive connection just as the target server is closing the | tries to use a keep-alive connection just as the target server is closing the | ||
connection (most likely because the server's keep-alive time limit was | connection (most likely because the server's keep-alive time limit was exceeded). | ||
exceeded). | |||
In such a case, the client side must try to use a different connection | In such a case, the client side must try to use a different connection | ||
to the target server or try to establish a new one. | to the target server or try to establish a new one. | ||
Line 84: | Line 94: | ||
In fact, the use of keep-alives for a <var class="product">Janus Sockets</var> HTTP client application should | In fact, the use of keep-alives for a <var class="product">Janus Sockets</var> HTTP client application should | ||
have no effect on application functionality. | have no effect on application functionality. | ||
==The HttpRequest class== | ==The HttpRequest class== | ||
<var>HttpRequest</var> is the object with which you construct and issue an HTTP request | <var>HttpRequest</var> is the object with which you construct and issue an HTTP request | ||
Line 89: | Line 100: | ||
'''To construct a request:''' Methods are provided, for example, for the following: | '''To construct a request:''' Methods are provided, for example, for the following: | ||
<table class=" | <table><tr class="head"><th>Task</th><th>Methods</th></tr> | ||
<tr><td>Setting and inspecting the request's URL</td> <td>Specifying the entire URL (<var>[[URL (HttpRequest property)|URL]]</var> method) or using individual methods to build the URL's constituent parts (TCP/IP protocol, target host, target port, target page, respectively with the <var>[[Protocol (HttpRequest property)|Protocol]]</var>, <var>[[Host (HttpRequest property)|Host]]</var>, <var>[[Port (HttpRequest property)|Port]]</var>, and <var>[[Page (HttpRequest property)|Page]]</var> methods) </td></tr> | <tr><td>Setting and inspecting the request's URL</td> <td>Specifying the entire URL (<var>[[URL (HttpRequest property)|URL]]</var> method) or using individual methods to build the URL's constituent parts (TCP/IP protocol, target host, target port, target page, respectively with the <var>[[Protocol (HttpRequest property)|Protocol]]</var>, <var>[[Host (HttpRequest property)|Host]]</var>, <var>[[Port (HttpRequest property)|Port]]</var>, and <var>[[Page (HttpRequest property)|Page]]</var> methods) </td></tr> | ||
Line 97: | Line 108: | ||
<tr><td>Adjusting HTTP header fields </td> <td><var>[[HeaderCount (HttpRequest function)|HeaderCount]]</var>, <var>[[AddHeader (HttpRequest subroutine)|AddHeader]]</var>, <var>[[AutoSendXmlContentType (HttpRequest property)|AutoSendXmlContentType]]</var> </td></tr> | <tr><td>Adjusting HTTP header fields </td> <td><var>[[HeaderCount (HttpRequest function)|HeaderCount]]</var>, <var>[[AddHeader (HttpRequest subroutine)|AddHeader]]</var>, <var>[[AutoSendXmlContentType (HttpRequest property)|AutoSendXmlContentType]]</var> </td></tr> | ||
<tr><td> | <tr><td>Providing Post data </td> <td><var>[[AddLongstring (HttpRequest subroutine)|AddLongstring]]</var>, <var>[[AddXml (HttpRequest subroutine)|AddXml]]</var></td></tr> | ||
<tr><td>Using a proxy server </td> <td><var>[[Proxy (HttpRequest property)|Proxy]]</var>, <var>[[RemoveProxy (HttpRequest subroutine)|RemoveProxy]]</var></td></tr> | <tr><td>Using a proxy server </td> <td><var>[[Proxy (HttpRequest property)|Proxy]]</var>, <var>[[RemoveProxy (HttpRequest subroutine)|RemoveProxy]]</var></td></tr> | ||
Line 103: | Line 114: | ||
<tr><td>Setting the HTTP protocol version </td> <td><var>[[HttpVersion (HttpRequest property)|HttpVersion]]</var></td></tr> | <tr><td>Setting the HTTP protocol version </td> <td><var>[[HttpVersion (HttpRequest property)|HttpVersion]]</var></td></tr> | ||
<tr><td>Handling redirection and requests for authentication </td><td><var>[[MaxRedirects (HttpRequest property)|MaxRedirects]]</var>, <var>[[SetAuthentication (HttpRequest subroutine)|SetAuthentication]]</var>, <var>[[RemoveAuthentication (HttpRequest subroutine)|RemoveAuthentication]]</var></td></tr> | <tr><td nowrap>Handling redirection and requests for authentication </td><td><var>[[MaxRedirects (HttpRequest property)|MaxRedirects]]</var>, <var>[[SetAuthentication (HttpRequest subroutine)|SetAuthentication]]</var>, <var>[[RemoveAuthentication (HttpRequest subroutine)|RemoveAuthentication]]</var></td></tr> | ||
</table> | </table> | ||
Line 111: | Line 122: | ||
The <var>[[Timeout (HttpRequest property)|Timeout]]</var> property sets a maximum wait for these operations to complete, while <var>[[Print (HttpRequest subroutine)|Print]]</var> provides a report of the request for debugging. | The <var>[[Timeout (HttpRequest property)|Timeout]]</var> property sets a maximum wait for these operations to complete, while <var>[[Print (HttpRequest subroutine)|Print]]</var> provides a report of the request for debugging. | ||
===List of HttpRequest methods=== | |||
The [[List of HttpRequest methods]] contains a complete list of the class methods. | |||
==The HttpResponse class== | ==The HttpResponse class== | ||
Line 130: | Line 142: | ||
<dl> | <dl> | ||
<dt>Status reporting | <dt>Status reporting | ||
<dd> | <dd>These methods take no parameters and let you check the | ||
These methods take no parameters and let you check the | |||
results of the <var>Get</var>, <var>Post</var>, | results of the <var>Get</var>, <var>Post</var>, | ||
or <var>Send</var> operation most recently performed. | or <var>Send</var> operation most recently performed. | ||
Line 164: | Line 174: | ||
<dd> | <dd> | ||
These methods allow the retrieval of HTTP | These methods allow the retrieval of HTTP response headers returned by an HTTP request. | ||
response headers returned by an HTTP request. | |||
<ul> | <ul> | ||
<li><var>[[HeaderCount (HttpResponse function)|HeaderCount]]</var> returns the number of headers returned by the server, or | <li><var>[[HeaderCount (HttpResponse function)|HeaderCount]]</var> returns the number of headers returned by the server, or | ||
the count of the number of occurrences of a particular header. | the count of the number of occurrences of a particular header. | ||
<li><var>[[HeaderNames (HttpResponse function)|HeaderNames]]</var> returns a <var>Stringlist</var> object listing the names of the HTTP response headers returned. | <li><var>[[HeaderNames (HttpResponse function)|HeaderNames]]</var> returns a <var>Stringlist</var> object listing the names of the HTTP response headers returned. | ||
<li><var>[[HeaderValue (HttpResponse function)|HeaderValue]]</var> returns the value of the header whose name is passed. | <li><var>[[HeaderValue (HttpResponse function)|HeaderValue]]</var> returns the value of the header whose name is passed. | ||
</ul> | </ul> | ||
</dl> | </dl> | ||
===List of HttpResponse methods=== | |||
The [[List of HttpResponse methods]] contains a complete list of the class methods. | |||
==Examples== | ==Examples== | ||
Examples follow in which <var>HttpRequest</var> and <var>HttpResponse</var> objects are used to | Examples follow in which <var>HttpRequest</var> and <var>HttpResponse</var> objects are used to | ||
send a request to a server and to process the document the server | send a request to a server and to process the document the server sends in response. | ||
sends in response. | |||
The first example uses HTTP GET; the second uses HTTP PUT. | The first example uses HTTP GET; the second uses HTTP PUT. | ||
Not shown is the <var class="product">Janus Sockets</var> <var>[[JANUS CLSOCK|CLSOCK]]</var> port definition and | Not shown is the <var class="product">Janus Sockets</var> <var>[[JANUS CLSOCK|CLSOCK]]</var> port definition and | ||
<var>[[JANUS CLSOCK#JANUS CLSOCK ALLOW|CLSOCK ALLOW]]</var> rules | <var>[[JANUS CLSOCK#JANUS CLSOCK ALLOW|CLSOCK ALLOW]]</var> rules | ||
for the socket ( | for the socket ("SOCKIT2ME") named in the examples. | ||
===HTTP GET example=== | ===HTTP GET example=== | ||
In this example, an XML document is fetched using HTTP GET. | In this example, an XML document is fetched using HTTP GET. | ||
<p class="code"> Begin | <p class="code">Begin | ||
Variables are undefined | |||
%httpreq object httpRequest | |||
%httpresp object httpResponse | |||
%mydoc object XmlDoc | |||
%rc float | |||
%httpreq = new | |||
%mydoc = new | |||
* Identify the target URL | |||
* (Prepare the request) | |||
%httpreq:URL = 'foo.com/bar' | |||
* Send request to server with HTTP GET | |||
%httpresp = %httpreq:Get('SOCKIT2ME') | |||
* Check the status and process the results returned | |||
If ( %httpresp:Success ) then | |||
%rc = %httpresp:ParseXML( %mydoc ) | |||
* the whole document is now in %mydoc | |||
* do whatever .... | |||
End If | |||
End | |||
</p> | </p> | ||
===HTTP POST example=== | ===HTTP POST example=== | ||
This example simulates a form submission using HTTP POST. | This example simulates a form submission using HTTP POST. | ||
<p class="code"> Begin | <p class="code">Begin | ||
Variables are undefined | |||
%httpreq object httpRequest | |||
%httpresp object httpResponse | |||
%mydoc is longstring | |||
%httpreq = new | |||
* Identify the target URL | |||
* (Prepare the request) | |||
%httpreq:URL = 'foo.com/bin/lookup' | |||
* Set the form fields | |||
%httpreq:AddField('FirstName','Moe') | |||
%httpreq:AddField('LastName','Howard') | |||
* Send request to server with HTTP POST | |||
* Do Not cancel if comm or socket error | |||
%httpresp = %httpreq:Post( 'SOCKIT2ME', 0 ) | |||
* Check the status and process the results returned | |||
If %httpresp eq null then | |||
Print 'No Connection: ' $STATUSD | |||
ElseIf (%httpresp:Success) eq 0 then | |||
Print 'ErrInfo: ' %httpresp:StatusLine | |||
Else | |||
%mydoc = %httpresp:Content | |||
* the whole document is now in %mydoc | |||
* and can be processed as appropriate | |||
End If | |||
End | |||
</p> | </p> | ||
==See also== | |||
<ul> | |||
<li>[[Janus Sockets User Language coding considerations]] | |||
<li>[[Sample Janus Sockets programs]] | |||
<li>Socket-level interfaces: | |||
<ul> | |||
<li>[[Janus Sockets $functions|$functions]] | |||
<li>[[Socket class]] | |||
</ul> | |||
<li>Higher-level interfaces: | |||
<ul> | |||
<li>[[Email class]] | |||
<li>[[HTTP Helper|HTTP Helper classes]] | |||
<li>[[LDAP class]] | |||
<li>[[Janus FTP Server]] | |||
<li>[[Janus Telnet Server]] | |||
</ul> | |||
[[Category:Janus Sockets]] | |||
[[Category:System classes|HTTP Helper classes]] |
Latest revision as of 00:13, 19 April 2015
The HTTP Helper is a high level, object oriented interface to client sockets that permits you to write a SOUL HTTP client without knowledge of socket level programming or the format of HTTP requests and responses.
The HTTP Helper consists of two object classes: HttpRequest and HttpResponse.
Note: To use the HTTP Helper objects under versions of Model 204 earlier than 7.5, you must have licensed Janus TCP/IP Base, Janus Sockets, and Janus SOAP. Under version 7.5 and later, you do not require Janus SOAP.
For information about using SOUL objects, see Object oriented programming in SOUL.
Feature summary
The following capabilities are provided by the HTTP Helper:
- Processing GET and POST HTTP requests, including XML support.
- Setting form fields and HTTP request headers and posting XML content.
- Sending arbitrary HTTP POST data (using a Longstring).
- Browser-like handling of file uploads using Multipart Form encoding
- After a request, accessing the HTTP Status line, the response headers, and the content.
- Accessing the content in its raw form and parsed into lines, or deserializing returned XML into an XmlDoc object.
- Optional automatic handling of redirects that a Get or Post method call receives from the server
- Using HTTPS, Secure Socket Layer (SSL) HTTP, as well as standard HTTP.
- Using HTTP Authentication to access password-protected URLs.
- Automatic and transparent translation from ASCII to and from EBCDIC.
- Using proxy servers.
- Using HTTP Version 1.1 or 1.0. Version 1.1 is the default.
Keep-Alive support
The client port definition parameter KEEPALIVE tells Janus Sockets to keep an HTTP connection to a web server open for a certain number of seconds so Janus Sockets can send another request on the same connection. This can reduce network traffic and, more significantly, HTTP request latency. Both of these benefits are magnified for SSL connections, where each HTTP request requires a TCP/IP and SSL connection-establishment handshake.
The Keepalive parameter must be followed by a single number between 1 and 32767 that indicates the number of seconds a TCP/IP connection is to be held open after an HTTP request on that connection. For the connection to be held open, the web server must indicate that it supports HTTP keep-alives. Most modern web servers can take advantage of keep-alives.
A keep-alive TCP/IP connection uses a Janus Sockets thread, so it is counted against both the maximum connections for a port and against a site's licensed-thread limits. Since a Janus Sockets application is often used to communicate with only a single or handful of web servers, this should not be a problem.
Keep-alives are highly specific to the HTTP protocol (there is a totally different keep-alive feature at the TCP protocol level with which HTTP keep-alives should not be confused), so Janus Sockets can only take advantage of them when it “knows” that HTTP is being used. This is only so when HTTP Helper objects (HttpRequest and HttpResponse) are used.
For an HttpRequest object Get or Post, if keep-alives are being used for a port (KEEPALIVE is non-zero on the port definition), Janus Sockets seeks an idle (keep-alive) connection for the target IP address. If one is found, that connection is used, avoiding connection establishment overhead. If none is found, a new connection is established. After the request is completed, rather than breaking the connection, it is held open for the KEEPALIVE timeout period. This approach reduces connection establishment overhead while in no way reducing parallelism — if a keep-alive connection is in use by one thread, another connection is used; if no unused ones are available, a new one is established.
There may be a timing problem with a keep-alive connection if a request tries to use a keep-alive connection just as the target server is closing the connection (most likely because the server's keep-alive time limit was exceeded). In such a case, the client side must try to use a different connection to the target server or try to establish a new one. This retry functionality is handled automatically and transparently by Janus Sockets. In fact, the use of keep-alives for a Janus Sockets HTTP client application should have no effect on application functionality.
The HttpRequest class
HttpRequest is the object with which you construct and issue an HTTP request to an HTTP server.
To construct a request: Methods are provided, for example, for the following:
Task | Methods |
---|---|
Setting and inspecting the request's URL | Specifying the entire URL (URL method) or using individual methods to build the URL's constituent parts (TCP/IP protocol, target host, target port, target page, respectively with the Protocol, Host, Port, and Page methods) |
Sending HTTP-form field and file data | FieldCount, AddField, LineEnd, MultiPartFormEncoding |
Adjusting HTTP header fields | HeaderCount, AddHeader, AutoSendXmlContentType |
Providing Post data | AddLongstring, AddXml |
Using a proxy server | Proxy, RemoveProxy |
Setting the HTTP protocol version | HttpVersion |
Handling redirection and requests for authentication | MaxRedirects, SetAuthentication, RemoveAuthentication |
To issue a request: HTTP method operations are provided by the Get, Post, and Send methods, which return an instance of the HttpResponse object. The Timeout property sets a maximum wait for these operations to complete, while Print provides a report of the request for debugging.
List of HttpRequest methods
The List of HttpRequest methods contains a complete list of the class methods.
The HttpResponse class
HttpResponse is an object that encapsulates the items returned from an HTTP request. When you call a Get, Post, or Send method in an HttpRequest, an instance of HttpResponse is created, populated, and returned. HttpResponse methods return the HTTP status code/message, the document itself, and any HTTP response headers.
Although a New method exists to instantiate an HttpResponse object, the Get, Post, and Send methods of the HttpRequest class instantiate and return an instance of an HttpResponse object. HttpResponse methods access what was returned from the server.
The HttpResponse methods are of three principal types:
- Status reporting
- These methods take no parameters and let you check the
results of the Get, Post,
or Send operation most recently performed.
- The StatusLine method returns the entire HTTP status line, which consists of a code, a blank, and a message (for example, "200 OK").
- Code returns only the code portion of the status line (for example, "200").
- Message returns only the message portion of the status line (for example, "OK").
- Success returns 1 (true) if the HTTP status var is in the 200s (success range), and 0 (false) otherwise. If Success is true, content was retrieved and is available via the Content and ParseXml methods.
- URL returns the value of the URL from which the Get, Post, or Send response is obtained.
- Accessing the returned document
-
These methods let you access the document/content from Get, Post, and Send
operations.
You call them
if the Success method indicates a
successful HTTP transaction has occurred.
- Content returns the entire document into a longstring with no parsing.
- ContentToStringlist returns the entire document into a Stringlist.
- ParseXML deserializes the returned data (assumed to be XML) into the XmlDoc that is passed.
- Response header
-
These methods allow the retrieval of HTTP response headers returned by an HTTP request.
- HeaderCount returns the number of headers returned by the server, or the count of the number of occurrences of a particular header.
- HeaderNames returns a Stringlist object listing the names of the HTTP response headers returned.
- HeaderValue returns the value of the header whose name is passed.
List of HttpResponse methods
The List of HttpResponse methods contains a complete list of the class methods.
Examples
Examples follow in which HttpRequest and HttpResponse objects are used to send a request to a server and to process the document the server sends in response. The first example uses HTTP GET; the second uses HTTP PUT. Not shown is the Janus Sockets CLSOCK port definition and CLSOCK ALLOW rules for the socket ("SOCKIT2ME") named in the examples.
HTTP GET example
In this example, an XML document is fetched using HTTP GET.
Begin Variables are undefined %httpreq object httpRequest %httpresp object httpResponse %mydoc object XmlDoc %rc float %httpreq = new %mydoc = new * Identify the target URL * (Prepare the request) %httpreq:URL = 'foo.com/bar' * Send request to server with HTTP GET %httpresp = %httpreq:Get('SOCKIT2ME') * Check the status and process the results returned If ( %httpresp:Success ) then %rc = %httpresp:ParseXML( %mydoc ) * the whole document is now in %mydoc * do whatever .... End If End
HTTP POST example
This example simulates a form submission using HTTP POST.
Begin Variables are undefined %httpreq object httpRequest %httpresp object httpResponse %mydoc is longstring %httpreq = new * Identify the target URL * (Prepare the request) %httpreq:URL = 'foo.com/bin/lookup' * Set the form fields %httpreq:AddField('FirstName','Moe') %httpreq:AddField('LastName','Howard') * Send request to server with HTTP POST * Do Not cancel if comm or socket error %httpresp = %httpreq:Post( 'SOCKIT2ME', 0 ) * Check the status and process the results returned If %httpresp eq null then Print 'No Connection: ' $STATUSD ElseIf (%httpresp:Success) eq 0 then Print 'ErrInfo: ' %httpresp:StatusLine Else %mydoc = %httpresp:Content * the whole document is now in %mydoc * and can be processed as appropriate End If End
See also
- Janus Sockets User Language coding considerations
- Sample Janus Sockets programs
- Socket-level interfaces:
- Higher-level interfaces: