The network request sending capability supports phones, 2-in-1 devices, tablets, and wearables. Starting from version 5.1.1(19), it is also supported on TVs.
Your app can send a synchronous HTTP request, where you can set information such as the request header and request body, and obtain an HTTP response from the server. This capability is used to obtain resources and supports interceptor-based request and response processing.
For details about the APIs, please refer to the API Reference.
| API | Description |
|---|---|
| Rcp_Response *HMS_Rcp_FetchSync(Rcp_Session *session, Rcp_Request *request, uint32_t *errCode); | Sends an HTTP request and directly obtains an HTTP response from the server. |
Import required modules on CPP.
#include "RemoteCommunicationKit/rcp.h" #include <cstdlib> #include <cstring> #include <cstdio> #include <thread>
Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)
librcp_c.so
Create a Request object. Replace https://www.example.com with the actual URL to request. (This code block should be inserted into the main function or other function sections for execution.)
const char *kHttpServerAddress = "https://www.example.com"; Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
Create a session. (This code block should be inserted into the main function or other function sections for execution.)
uint32_t errCode = 0; Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
Initiate a request and process the returned result. (This code block should be inserted into the main function or other function sections for execution.)
Rcp_Response *response = HMS_Rcp_FetchSync(session, request, &errCode);
if (response != NULL) {
printf("Response status: %d\n", response->statusCode);
// ...
} else {
printf("FetchSync failed: errCode: %u\n", errCode);
// ...
} Destroy the response and request, and close the session. (This code block should be inserted into the main function or other function sections for execution.)
// Destroy the request.
HMS_Rcp_DestroyRequest(request);
// Process and destroy the response.
if (response != NULL) {
response->destroyResponse(response);
}
// Close the session.
errCode = HMS_Rcp_CloseSession(&session);
// Process errCode. Your app can send an asynchronous HTTP request, where you can set information such as the request header and request body, and obtain an HTTP response from the server. This capability is used to obtain resources and supports interceptor-based request and response processing.
For details about the APIs, please refer to the API Reference.
| API | Description |
|---|---|
| uint32_t HMS_Rcp_Fetch(Rcp_Session *session, Rcp_Request *request, const Rcp_ResponseCallbackObject *responseCallback); | Sends an HTTP request and obtains an HTTP response from the server. This API returns the result asynchronously through responseCallback. |
Import required modules on CPP.
#include "RemoteCommunicationKit/rcp.h" #include <cstdlib> #include <cstring> #include <cstdio> #include <thread>
Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)
librcp_c.so
Create a session and initiate a Fetch request. Replace https://www.example.com with the actual URL to request. After the response is returned, destroy the request and close the session.
// Create a request.
const char *kHttpServerAddress = "https://www.example.com";
Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
ctx->request = request;
uint32_t errCode = 0;
// Create a session.
Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
ctx->session = session;
// Request configuration.
Rcp_Configuration config;
// Initialize parameter settings.
(void)memset(&config, 0, sizeof(Rcp_Configuration));
// Reconfigure automatic redirection.
config.transferConfiguration.autoRedirect = true;
// Reconfigure request timeout parameters.
config.transferConfiguration.timeout.transferMs = 1000 * 10;
config.transferConfiguration.timeout.connectMs = 1000 * 10;
request->method = RCP_METHOD_GET;
request->configuration = &config;
// Configure the request callback.
Rcp_ResponseCallbackObject responseCallback = {ResponseCallback, ctx};
// Initiate a request.
errCode = HMS_Rcp_Fetch(session, request, &responseCallback); Your app can send an HTTP GET request with the default HTTP parameters and obtain an HTTP response from the server. Then an asynchronous callback is used to enhance the responsiveness and efficiency of your app. Such requests are usually used to obtain data from the server.
Import required modules on CPP.
#include "RemoteCommunicationKit/rcp.h" #include <cstdlib> #include <cstring> #include <cstdio> #include <thread>
Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)
librcp_c.so
Create a session and initiate a GET request. Replace https://www.example.com with the actual URL to request. After the response is returned, destroy the request and close the session.
const char *kHttpServerAddress = "https://www.example.com";
// Request configuration.
Rcp_Configuration config;
// Initialize parameter settings.
(void)memset(&config, 0, sizeof(Rcp_Configuration));
// Reconfigure automatic redirection.
config.transferConfiguration.autoRedirect = true;
// Reconfigure request timeout parameters.
config.transferConfiguration.timeout.transferMs = 1000 * 10;
config.transferConfiguration.timeout.connectMs = 1000 * 10;
Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
ctx->request = request;
request->method = RCP_METHOD_GET;
request->configuration = &config;
uint32_t errCode = 0;
// Create a session.
Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
ctx->session = session;
// Configure the request callback.
Rcp_ResponseCallbackObject responseCallback = {ResponseCallback, ctx};
// Initiate a request.
errCode = HMS_Rcp_Fetch(session, request, &responseCallback); Your app can send an HTTP POST request with the default HTTP parameters and obtain an HTTP response from the server. The result will be returned asynchronously. This capability is used to submit data to the server. Unlike a GET request, in a POST request, the request body carries parameters. Such requests can be used to create resources, submit form data, or execute specific operations.
Import required modules on CPP.
#include "RemoteCommunicationKit/rcp.h" #include <cstdlib> #include <cstring> #include <cstdio> #include <thread>
Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)
librcp_c.so
Create a session and initiate a POST request. Replace https://www.example.com with the actual URL to request. After the response is returned, destroy the request and close the session.
// Create a request.
const char *kHttpServerAddress = "http://www.example.com";
const char *content = "content";
Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
// Set request parameters.
request->method = RCP_METHOD_POST;
request->content = (Rcp_RequestContent *)calloc(1, sizeof(Rcp_RequestContent));
request->content->type = RCP_CONTENT_TYPE_STRING;
request->content->data.contentStr.buffer = content;
request->content->data.contentStr.length = strlen(content);
ctx->request = request;
// Request configuration.
Rcp_Configuration config;
// Initialize parameter settings.
(void)memset(&config, 0, sizeof(Rcp_Configuration));
// Reconfigure automatic redirection.
config.transferConfiguration.autoRedirect = true;
// Reconfigure request timeout parameters.
config.transferConfiguration.timeout.transferMs = 1000 * 10;
config.transferConfiguration.timeout.connectMs = 1000 * 10;
request->configuration = &config;
uint32_t errCode = 0;
// Create a session.
Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
ctx->session = session;
// Configure the request callback.
Rcp_ResponseCallbackObject responseCallback = {ResponseCallback, ctx};
// Initiate a request.
errCode = HMS_Rcp_Fetch(session, request, &responseCallback); Your app can send an HTTP PUT request with the default HTTP parameters and obtain an HTTP response from the server. The result will be returned asynchronously. Such requests are usually used to update resources on the server by sending new content of a resource to a specified URL to replace all content of the resource on the server.
Import required modules on CPP.
#include "RemoteCommunicationKit/rcp.h" #include <cstdlib> #include <cstring> #include <cstdio> #include <thread>
Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)
librcp_c.so
Create a session and initiate a PUT request. Replace <idp:inline displayname="code" id="code16611111171911">https://www.example.com with the actual URL to request. After the response is returned, destroy the request and close the session.
const char *kHttpServerAddress = "http://www.example.com";
const char *content = "content";
// Create a request and set request parameters.
Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
request->method = RCP_METHOD_PUT;
request->content = (Rcp_RequestContent *)calloc(1, sizeof(Rcp_RequestContent));
request->content->type = RCP_CONTENT_TYPE_STRING;
request->content->data.contentStr.buffer = content;
request->content->data.contentStr.length = strlen(content);
ctx->request = request;
// Request configuration.
Rcp_Configuration config;
// Initialize parameter settings.
(void)memset(&config, 0, sizeof(Rcp_Configuration));
// Reconfigure automatic redirection.
config.transferConfiguration.autoRedirect = true;
// Reconfigure request timeout parameters.
config.transferConfiguration.timeout.transferMs = 1000 * 10;
config.transferConfiguration.timeout.connectMs = 1000 * 10;
request->configuration = &config;
uint32_t errCode = 0;
// Create a session.
Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
ctx->session = session;
// Configure the request callback.
Rcp_ResponseCallbackObject responseCallback = {ResponseCallback, ctx};
// Initiate a FETCH request.
errCode = HMS_Rcp_Fetch(session, request, &responseCallback); Your app can send an HTTP HEAD request with the default HTTP parameters and obtain an HTTP response from the server. The result will be returned asynchronously. The HEAD method is similar to the GET method, except that the response to a HEAD request contains only the header. The request obtains resource metadata, such as the file size and modification date.
Import required modules on CPP.
#include "RemoteCommunicationKit/rcp.h" #include <cstdlib> #include <cstring> #include <cstdio> #include <thread>
Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)
librcp_c.so
Create a session and initiate a HEAD request. Replace <idp:inline displayname="code" id="code1966181181911">https://www.example.com with the actual URL to request. After the response is returned, destroy the request and close the session.
const char *kHttpServerAddress = "http://www.example.com/head";
Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
request->method = RCP_METHOD_HEAD;
// Request configuration.
Rcp_Configuration config;
// Initialize parameter settings.
(void)memset(&config, 0, sizeof(Rcp_Configuration));
// Reconfigure automatic redirection.
config.transferConfiguration.autoRedirect = true;
// Reconfigure request timeout parameters.
config.transferConfiguration.timeout.transferMs = 1000 * 10;
config.transferConfiguration.timeout.connectMs = 1000 * 10;
request->configuration = &config;
ctx->request = request;
uint32_t errCode = 0;
// Create a session.
Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
ctx->session = session;
// Configure the request callback.
Rcp_ResponseCallbackObject responseCallback = {ResponseCallback, ctx};
// Initiate a FETCH request.
errCode = HMS_Rcp_Fetch(session, request, &responseCallback); Your app can send an HTTP DELETE request with the default HTTP parameters to delete a resource from the server by specifying the resource URL and obtain an HTTP response from the server. The result will be returned asynchronously.
Import required modules on CPP.
#include "RemoteCommunicationKit/rcp.h" #include <cstdlib> #include <cstring> #include <cstdio> #include <thread>
Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)
librcp_c.so
Create a session and initiate a DELETE request. Replace <idp:inline displayname="code" id="code26611911181912">https://www.example.com with the actual URL to request. After the response is returned, destroy the request and close the session.
const char *kHttpServerAddress = "http://www.example.com/delete";
Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
request->method = RCP_METHOD_DELETE;
// Request configuration.
Rcp_Configuration config;
// Initialize parameter settings.
(void)memset(&config, 0, sizeof(Rcp_Configuration));
// Reconfigure automatic redirection.
config.transferConfiguration.autoRedirect = true;
// Reconfigure request timeout parameters.
config.transferConfiguration.timeout.transferMs = 1000 * 10;
config.transferConfiguration.timeout.connectMs = 1000 * 10;
request->configuration = &config;
ctx->request = request;
uint32_t errCode = 0;
// Create a session.
Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
ctx->session = session;
// Configure the request callback.
Rcp_ResponseCallbackObject responseCallback = {ResponseCallback, ctx};
// Initiate a FETCH request.
errCode = HMS_Rcp_Fetch(session, request, &responseCallback);