In the framework of Remote Communication Kit, you can use ProxyConfiguration to configure session proxy settings. The following proxy options are supported:
system: uses the system proxy, which is suitable for scenarios where quick deployment is required.
no-proxy: uses no proxy, which is suitable for scenarios where direct access to specified resources is required or proxy restrictions exist.
WebProxy: customizes proxy settings to resolve specific network issues, optimize proxy routing, and improve performance and user experience.
The proxy customization capability supports phones, 2-in-1 devices, tablets, and wearables. Starting from version 5.1.1(19), it is also supported on TVs.
The following illustrates the sample code of the three proxy options (system, no-proxy, and WebProxy).
Import the required modules.
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit'; Create a new session object and a request. The session is used to manage subsequent network requests.
const session = rcp.createSession(); // Define the request URL. (Change the value in the example based on your needs.) const requestURL = 'https://example.com';
Define the proxy settings for initiating requests. You can choose the no-proxy option in proxyConfiguration to specify the proxy type as not using any proxy.
// Set the request proxy mode to no-proxy.
const configuration: rcp.Configuration = {
proxy: 'no-proxy'
}
// Define the request and add the request configuration to the request.
const request = new rcp.Request(requestURL, 'GET');
request.configuration = configuration; Use FETCH to initiate a network request and handle the response on success or failure. The following is just an example, and the processing of success or failure should be implemented based on your service needs.
session.fetch(request).then((response: rcp.Response) => {
// Process the success response.
console.info(`Response success, ${response}`);
// ...
// Close the session.
session.close();
}).catch((err: BusinessError) => {
// Process the failure response.
console.error(`The error code is ${err.code}, error data is ${err.data}`);
// ...
// Close the session.
session.close();
}) Import the required modules.
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit'; Create a new session object and a request. The session is used to manage subsequent network requests.
const session = rcp.createSession(); // Define the request URL. (Change the value in the example based on your needs.) const requestURL = 'https://example.com';
Define the proxy settings for initiating requests. You can choose the system option in proxyConfiguration to use the system proxy.
// Set the request proxy mode to system.
const configuration: rcp.Configuration = {
proxy: 'system'
}
// Define the request and add the request configuration to the request.
const request = new rcp.Request(requestURL, 'GET');
request.configuration = configuration; Use FETCH to initiate a network request and handle the response on success or failure. The following is just an example, and the processing of success or failure should be implemented based on your service needs.
session.fetch(request).then((response: rcp.Response) => {
// Process the success response.
console.info(`Response success, ${response}`);
// ...
// Close the session.
session.close();
}).catch((err: BusinessError) => {
// Process the failure response.
console.error(`The error code is ${err.code}, error data is ${err.data}`);
// ...
// Close the session.
session.close();
}) Import the required modules.
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit'; Create a new session object and a request. The session is used to manage subsequent network requests.
const session = rcp.createSession(); // Define the request URL. (Change the value in the example based on your needs.) const requestURL = 'https://example.com';
Define custom proxy configurations declaratively with WebProxy.
// Set the proxy. (Modify it as needed.)
const configuration: rcp.Configuration = {
proxy: {
url: 'https://www.example.com',
createTunnel: 'always'
}
}
// Define the request and add the request configuration to the request.
const request = new rcp.Request(requestURL, 'GET');
request.configuration = configuration; Use FETCH to initiate a network request and handle the response on success or failure. The following is just an example, and the processing of success or failure should be implemented based on your service needs.
session.fetch(request).then((response: rcp.Response) => {
// Process the success response.
console.info(`Response success, ${response}`);
// ...
// Close the session.
session.close();
}).catch((err: BusinessError) => {
// Process the failure response.
console.error(`The error code is ${err.code}, error data is ${err.data}`);
// ...
// Close the session.
session.close();
})