SocketTools .NET Edition

HttpClient.PostJson Method (String, String, String, HttpPostOptions)

Submits JSON formatted data to the server and returns the result in a string.

[Visual Basic]
Overloads Public Function PostJson( _
   ByVal resourceName As String, _
   ByVal jsonData As String, _
   ByRef responseData As String, _
   ByVal options As HttpPostOptions _
) As Boolean
[C#]
public bool PostJson(
   string resourceName,
   string jsonData,
   ref string responseData,
   HttpPostOptions options
);

Parameters

resourceName
A string that specifies the resource on the server that the data will be posted to. Typically this is the name of an executable script on the server. The resource name should be specified using an absolute path that begins with a leading slash character.
jsonData
A string that contains the JSON formatted data which will be provided to the script.
responseData
A string passed by reference that will contain the output generated by the script. Typically this is HTML content which is generated by the script as a result of processing the data that was posted to it.
options
An HttpPostOptions enumeration that specifies one or more options when posting data to the server.

Return Value

This method returns a boolean value. If the method succeeds, the return value is true. If the method fails, the return value is false. To get extended error information, check the value of the LastError property.

Remarks

The PostJson method is used to submit JSON formatted data to a script that executes on the server and then copy the output from that script into a local buffer. This function automatically sets the correct content type and encoding required for submitting JSON data to a server, however it does not parse the JSON data itself to ensure that it is well-formed. Your application is responsible for ensuring that the JSON data that is being submitted to the server is formatted correctly.

When encountering a server error during a request, the PostJson method normally returns false, and no data is copied into the caller-provided buffer. The LastError property is updated to reflect the general cause of the failure, allowing the application to handle this error condition appropriately. Additionally, servers may provide further details about the failure, such as XML or JSON formatted data containing specific error codes or diagnostic messages.

To capture this error information, you can utilize the postErrorData option. When this option is enabled, the behavior of PostJson changes; it does not return false for server error statuses. Instead, any error data provided in the server's response, regardless of its format, is copied into the buffer provided by the caller.

This method will cause the current thread to block until the operation completes, a timeout occurs or the post is canceled. During the operation, the OnProgress event will fire periodically, enabling the application to update any user interface objects such as a progress bar.

See Also

HttpClient Class | SocketTools Namespace | HttpClient.PostJson Overload List