Summary:
See also: The Genero Web Services COM Library
The HTTPRequest class provides an interface to perform asynchronous XML and TEXT requests over HTTP for a specified URL, with additional XML streaming possibilities.
The status is set to zero after a successful method call.
com.HTTPRequest
Class Methods | |
Name | Description |
com.HTTPRequest.Create(
|
Creates an
HTTPRequest object by providing a mandatory url
with HTTP or HTTPS as the protocol. Note: url can be an identifier of an URL mapping with an optional alias:// prefix. See FGLPROFILE Configuration for more details about URL mapping with aliases, and for proxy and security configuration. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
setVersion(
|
Sets the HTTP version
of the request. Accepted versions are 1.0 and 1.1 (only these two versions are supported). The default is 1.1. Throws an exception in case of errors, and updates status with an error code. |
setMethod(
|
Sets the HTTP method
of the request. Supported methods are GET, PUT, POST, HEAD and DELETE. The default is GET. Throws an exception in case of errors, and updates status with an error code. |
setHeader(
|
Sets an HTTP
header name and value for the
request, and replaces the previous one if there was any. Setting a header after the body has been sent, or if a streaming operation has been started, will only be taken into account when a new request is reissued. Throws an exception in case of errors, and updates status with an error code. |
removeHeader(
|
Removes an HTTP
header name for the request if it exists. Throws an exception in case of errors, and updates status with an error code. |
clearHeaders()
|
Removes all user-defined headers. |
setCharset(
|
Defines the charset
used when sending text or XML; by default no charset is set. When sending text, HTTP specification defines ISO-8859-1 as an implicit charset. When sending XML, the user-defined charset is used instead of the one set in the XML document itself, which can lead to a charset conversion error at the server side. It is recommended that you unset it by setting charset to NULL, or that you use the same charset that was set in the XML Document. |
setAuthentication(
|
Defines the
mandatory user login and password
to authenticate to the server. An optional scheme defines the method to be used during authentication. Only Anonymous, Basic and Digest are supported. The default is Anonymous. An optional realm can be set.
Throws an exception in case of errors, and updates status with an error code. |
clearAuthentication() |
Removes
user-defined authentication. If an authenticate entry exists in the FGLPROFILE file, it will be used for authentication, even if the user-defined authentication was removed. |
setKeepConnection( |
Defines whether
the connection should stay open if a new request occurs
again. The default is FALSE. |
setTimeOut( |
Sets the time
value in seconds to wait for a reading or writing operation, before a
break. The value of -1 means infinite wait. |
setConnectionTimeOut( |
Sets the time
value in seconds to wait for the establishment of the connection,
before a break. The value of -1 means infinite wait. |
setMaximumResponseLength( |
Sets the maximum
authorized size in Kbyte of the whole response
(composed of the headers, the body and all control characters), before
a break. The value of -1 means no limit. |
setAutoReply( |
Defines whether method getReponse()
or getAsyncResponse()
will automatically perform another HTTP GET request if response
contains HTTP Authentication, Proxy Authentication or HTTP
redirect data. The efault is TRUE. Note: Only available for GET method. |
Object Methods | |
Name | Description |
doRequest() |
Performs the
request. Supported methods are GET, HEAD and DELETE. Throws an exception in case of errors, and updates status with an error code. |
doTextRequest( |
Performs the
request by sending an entire string at once. Supported methods
are PUT and POST.
Throws an exception in case of errors, and updates status with an error code. |
doXmlRequest( |
Performs the
request by sending the entire xml.DomDocument
at once. Supported methods are PUT and POST.
Throws an exception in case of errors, and updates status with an error code. |
doFormEncodedRequest( |
Performs an application/x-www-form-urlencoded
Forms encoded query. Supported methods are GET and POST. The query
string is a list of name/value pairs separated with &. For
example, name1=value1&name2=value2&name3=value3. If utf8 is TRUE, the query string is encoded in UTF-8 as specified in XForms 1.0, otherwise in ASCII as specified in HTML 4. Note : if you need to url encode the separator characters & and =, simply double them as following : na&&me=va==lue . Throws an exception in case of errors, and updates status with an error code. |
beginXmlRequest() |
Begins the
streaming HTTP request and returns an xml.StaxWriter object
ready to send XML to the server. Supported methods are PUT and
POST.
Throws an exception in case of errors, and updates status with an error code. |
endXmlRequest( |
Ends the
streaming HTTP request by closing the Stax writer. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
getResponse() |
Returns the
response of one of the doRequest, doTextRequest,
doXmlRequest, doFormEncodedRequest
or beginXmlRequest and endXmlRequest calls in an com.HTTPResponse
object. Throws an exception in case of errors, and updates status with an error code. |
getAsyncResponse() |
Returns the
response of one of the doRequest, doTextRequest,
doXmlRequest, doFormEncodedRequest
or beginXmlRequest and endXmlRequest calls in an com.HTTPResponse
object, or NULL if the response was not yet received. If a previous call returned NULL, a new call will return the expected response if it has already arrived, or NULL again if the response was still not received. Throws an exception in case of errors, and updates status with an error code. |
01
IMPORT com02
03
MAIN04
DEFINE req com.HTTPRequest05
DEFINE resp com.HTTPResponse06
TRY07
LET req = com.HTTPRequest.Create("http://localhost:8090/MyPage")08
CALL req.setHeader("MyHeader","High Priority") # Set additional HTTP header with name 'MyHeader', and value 'High Priority'09
CALL req.doRequest()10
LET resp = req.getResponse()11
IF resp.getStatusCode() != 200 THEN12
DISPLAY "HTTP Error ("||resp.getStatusCode()||") ",resp.getStatusDescription()13
ELSE14
DISPLAY "HTTP Response is : ",resp.getTextResponse()15
END IF16
CATCH17
DISPLAY "ERROR :",STATUS||" ("||SQLCA.SQLERRM||")"18
END TRY19
END MAIN
01
IMPORT com02
IMPORT xml03
04
MAIN05
DEFINE req com.HTTPRequest06
DEFINE resp com.HTTPResponse07
DEFINE doc xml.DomDocument08
TRY09
LET req = com.HTTPRequest.Create("http://localhost:8090/MyProcess")10
CALL req.setMethod("POST") # Perform an HTTP POST method11
CALL req.doFormEncodedRequest("Param1=hello&Param2=how are you ?",FALSE) # Param1 value is 'hello', Param2 value is 'how are you ?'12
LET resp = req.getResponse()13
IF resp.getStatusCode() != 200 THEN14
DISPLAY "HTTP Error ("||resp.getStatusCode()||") ",resp.getStatusDescription()15
ELSE16
LET doc = resp.getXmlResponse() # Expect a returned content type of the form */xml17
DISPLAY "HTTP XML Response is : ",doc.saveToString()18
END IF19
CATCH20
DISPLAY "ERROR :",STATUS||" ("||SQLCA.SQLERRM||")"21
END TRY22
END MAIN
01
IMPORT com02
IMPORT xml03
04
MAIN05
DEFINE req com.HTTPRequest06
DEFINE resp com.HTTPResponse07
DEFINE writer xml.StaxWriter08
TRY09
LET req = com.HTTPRequest.Create("http://localhost:8090/MyXmlProcess")10
CALL req.setMethod("PUT") # Perform an HTTP PUT method11
CALL req.setHeader("MyHeader","Value of my header")12
LET writer = req.beginXmlRequest() # Retrieve an xml.StaxWriter to start xml streaming13
CALL writer.startDocument("utf-8","1.0",true)14
CALL writer.comment("My first XML document sent in streaming with genero!!!")15
CALL writer.startElement("root")16
CALL writer.attribute("attr1","value1")17
CALL writer.endElement()18
CALL writer.endDocument()19
CALL req.endXmlRequest(writer) # End streaming request20
LET resp = req.getResponse()21
IF resp.getStatusCode() != 201 OR resp.getStatusCode() != 204 THEN22
DISPLAY "HTTP Error ("||resp.getStatusCode()||") ",resp.getStatusDescription()23
ELSE24
DISPLAY "XML document was correctly put on the server"25
END IF26
CATCH27
DISPLAY "ERROR :",STATUS||" ("||SQLCA.SQLERRM||")"28
END TRY29
END MAIN
01
IMPORT com02
03
MAIN04
DEFINE req com.HTTPRequest05
DEFINE resp com.HTTPResponse06
DEFINE url STRING07
DEFINE quit CHAR(1)08
DEFINE questionStr STRING09
DEFINE timeout INTEGER10
TRY11
WHILE TRUE12
PROMPT "Enter http url you want to delete ? " FOR url ATTRIBUTE (CANCEL=FALSE)13
LET req = com.HTTPRequest.Create(url)14
CALL req.setMethod("DELETE")15
CALL req.doRequest()16
LET resp = req.getAsyncResponse() # Retrieve asynchronous response for the first time17
CALL Update(resp) RETURNING questionStr,timeout18
WHILE quit IS NULL OR ( quit!="Y" AND quit!="N" )19
PROMPT questionStr FOR CHAR quit ATTRIBUTE (CANCEL=FALSE,ACCEPT=FALSE,SHIFT="up")20
ON IDLE timeout21
IF resp IS NULL THEN # If no response at first try, retrieve it again22
LET resp = req.getAsyncResponse() # because we have time now !!!23
CALL Update(resp) RETURNING questionStr,timeout24
END IF25
END PROMPT26
END WHILE27
IF quit == "Y" THEN28
EXIT PROGRAM29
ELSE30
LET quit = NULL31
END IF32
END WHILE33
CATCH34
DISPLAY "ERROR :",STATUS,SQLCA.SQLERRM35
END TRY36
END MAIN37
38
FUNCTION Update(resp)39
DEFINE resp com.HTTPResponse40
DEFINE ret STRING41
IF resp IS NOT NULL THEN42
IF resp.getStatusCode() != 204 THEN43
LET ret = "HTTP Error ("||resp.getStatusCode()||") :"||resp.getStatusDescription()||". Do you want to quit ? "44
ELSE45
LET ret = "HTTP Page deleted. Do you want to quit ? "46
END IF47
RETURN ret, 048
ELSE49
LET ret = "Do you want to quit ? "50
RETURN ret, 151
END IF52
END FUNCTION