| |
| INT PostText( |
| |
LPCTSTR lpszResource, |
|
| |
LPCTSTR lpszTextData, |
|
| |
LPBYTE lpResult, |
|
| |
LPDWORD lpcbResult, |
|
| |
DWORD dwOptions |
|
| ); |
| INT PostText( |
| |
LPCTSTR lpszResource, |
|
| |
LPCTSTR lpszTextData, |
|
| |
HGLOBAL *lpResult, |
|
| |
LPDWORD lpcbResult, |
|
| |
DWORD dwOptions |
|
| ); |
| INT PostText( |
| |
LPCTSTR lpszResource, |
|
| |
LPCTSTR lpszTextData, |
|
| |
LPTSTR lpszResult, |
|
| |
LPDWORD lpcchResult, |
|
| |
UINT nCodePage, |
|
| |
DWORD dwOptions |
|
| ); |
| INT PostText( |
| |
LPCTSTR lpszResource, |
|
| |
LPCTSTR lpszTextData, |
|
| |
CString& strResult, |
|
| |
UINT nCodePage, |
|
| |
DWORD dwOptions |
|
| ); |
The PostText method submits unstructured text data to a script
on the server and returns the result in a buffer provided by the
caller.
Parameters
- lpszResource
- A null-terminated string that specifies the resource name the
text data will be submitted to. Typically this is the name of a
REST API endpoint or script on the server.
- lpszTextData
- A null-terminated string which specifies the
text to be submitted to the server. If the Unicode version of
this function is called, the text will sent as a UTF-8 encoded
string.
- lpvResult
- A buffer which will contain the data returned
by the server, or a pointer to a global memory handle which will
reference the data when the method returns.
- lpcbResult
- A pointer to an unsigned integer which should be initialized
to the maximum number of bytes that can be copied to the buffer
specified by the lpvResult parameter. If the lpvResult
parameter points to a global memory handle, the length value
should be initialized to zero. When the method returns, this
value will be updated with the number of bytes of data returned
by the server.
- strResult
- An alternate version of this method accepts a reference to
a CString object which will contain the
response from the server when the method returns. If the method
fails, an empty string will be returned. This version of the method
should only be used when you are certain the response from the
server will not contain binary data. If the Unicode version of this
method is called, it will automatically convert a UTF-8 encoded
response to a UTF-16 Unicode string.
- nCodePage
- An optional unsigned integer that specifies the ANSI code page
which should be used to convert the text returned by the server.
A value of zero specifies the default system code page should be used
with the conversion. In most cases, applications should use CP_UTF8
as the code page. See the remarks below for more information.
- dwOptions
- An unsigned integer that specifies one or more options. This
parameter is constructed by using a bitwise operator with any of
the following values:
| Value |
Description |
| HTTP_POST_DEFAULT |
If the data being returned from the server is textual, it
is automatically converted so that the end of line character
sequence is compatible with the Windows platform. Individual
carriage return or linefeed characters are converted to
carriage return/linefeed character sequences. This option
does not have any effect on the data being submitted to the
server, only on the data returned by the server. |
| HTTP_POST_ERRORDATA |
This option causes the client to accept error data from the
server if the request fails. If this option is specified, an
error response from the server will not cause the method to
fail. Instead, the response is returned to the client and the
method will succeed. |
Return Value
If the method succeeds, the return value is the server result
code. If the method fails, the return value is HTTP_ERROR. To get
extended error information, call GetLastError.
Remarks
The PostText method is used to submit plain
text to a REST API endpoint or script and then returns the output in
a local buffer. This method can be used to submit any kind of text
payload to the server, and will automatically sets a default content
type without any encoding, ensuring the text is submitted exactly as
specified. If the Unicode version of this method is called, the text
will be converted to UTF-8 prior to being submitted to the server. The
endpoint or script on the server side must be capable of processing
UTF-8 encoded text correctly or you may experience unexpected results.
If the content type for the request has not been explicitly
specified, it will be automatically updated by this method to use
the generic "text/plain" content type. You can override the
default content type for the request by calling the SetHeader
method prior to calling this method. Most servers require you to
explicitly specify what type of data is being submitted by the client
and will reject requests which do not correctly identify the content
type. Your application should ensure any content type you specify is
valid for the contents of the payload; the content type is not checked
against the text you submit to the server.
It is important for your application to initialize the value of the
lpcbResult parameter correctly. This parameter must be passed
by reference and if it is not initialized properly it can cause
unexpected behavior or corrupt the process heap. If an error occurs
when issuing the POST request to the server, the variable passed to
this method may have a value of zero when the method returns,
indicating no data has been returned to the caller.
If you use a version of this method which return the response in a
string buffer, you should typically specify the nCodePage
parameter as CP_UTF8 because this is what most servers use as
their default character set. If the server is returning text using a
character set other than UTF-8, specifying a code page enables your
application to automatically normalize that text to Unicode. It is
important to note the text returned by this function will always be
either UTF-16 or UTF-8 Unicode to ensure there is no data loss during
the conversion process.
When encountering a server error during a request, the
PostText method normally returns HTTP_ERROR, and no data is
copied into the caller-provided buffer. The last error code for the
current thread is updated to reflect the general cause of the failure,
allowing the application to handle this error condition appropriately.
If the POST request fails, servers may also provide further details
about the failure, such as XML or JSON formatted text containing
specific error codes or diagnostic messages.
To capture this error information, you can utilize the
HTTP_POST_ERRORDATA option. When this option is enabled, the
behavior of PostText changes; it does not return HTTP_ERROR
for server error statuses. Instead, any error data provided in the
server's response, regardless of its format, is copied into the result
buffer provided by the caller. If this option is used, your application
should call GetResultCode to obtain the HTTP status code
returned by the server. This will enable you to determine if the
operation was successful.
This method will cause the current thread to block until the POST
completes, a timeout occurs or the operation is canceled. To prevent
the request from blocking the main application thread, create a
background worker thread to submit the text payload. Connections
established in different threads will not interfere with one another
and will not cause the main thread to block while it waits for the
server to respond.
Example
HGLOBAL hgblBuffer = (HGLOBAL)NULL;
DWORD cbBuffer = 0;
nResult = lpClient->PostText(lpszResource,
lpszTextData,
&hgblBuffer,
&cbBuffer,
HTTP_POST_DEFAULT);
if (nResult != HTTP_ERROR)
{
LPBYTE lpBuffer = (LPBYTE)GlobalLock(hgblBuffer);
GlobalUnlock(hgblBuffer);
GlobalFree(hgblBuffer);
}
Requirements
Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtpv12.lib
Unicode: Implemented as Unicode and ANSI versions
See Also
|
|