DEV Community

Cover image for [Learn HarmonyOS Next Knowledge Every Day] Network-related Knowledge
kouwei qing
kouwei qing

Posted on

[Learn HarmonyOS Next Knowledge Every Day] Network-related Knowledge

[Learn HarmonyOS Next Knowledge Every Day] Network-related Knowledge

1. What is the difference between the HTTP native library and RCP?

Currently, the RCP capability is recommended, which offers better interface usability, performance, and power consumption compared to the HTTP native network library.

The underlying capabilities of the HTTP native network library are implemented via curl, while the underlying capabilities of the RCP module differ from the native HTTP and are mainly developed and closed-source packaged internally.

2. How to handle error code err number = 8 in the request.downloadFile interface?

The request.downloadFile often returns err number = 8, and the callback err=8 in downloadTask.on('fail', (err: number)) represents the unknown error described in the documentation.

An err=8 in the downloadFile fail callback indicates that the task failed due to an internal error. You can try retrying the task. Alternatively, in version 5.x, use the related interfaces of request.agent to obtain Faults, which will contain more specific information.

3. How to set the timeout period when initiating a request with the RCP library?

In the RCP library, relevant capabilities allow setting parameters in SessionConfiguration before establishing a session. For usage, refer to the following:

const sessionConfig: rcp.SessionConfiguration = {
  // Used to specify the configuration for HTTP requests associated with the session
  requestConfiguration: {
    transfer: {
      // Timeout parameter settings
      timeout: {
        // Connection timeout
        connectMs: 5000,
        // Transfer timeout
        transferMs: 10000,
      },
    }
  }
};
const session = rcp.createSession(sessionConfig);
Enter fullscreen mode Exit fullscreen mode

4. Error when using synchronous methods to get network status in a no-network environment?

In a no-network environment, calling synchronous methods to request cannot parse and obtain the corresponding content of nethandle, and an error occurs when the method internally executes getCap. You can use a try-catch approach to get the error information:

try {
  let netHandle = connection.getDefaultNetSync();
  let connectionproperties = connection.getConnectionPropertiesSync(netHandle);
} catch(err) {
  console.info('error: ' + JSON.stringify(err));
}
Enter fullscreen mode Exit fullscreen mode

5. How to monitor and determine VPN-type networks?

The VPN type can be obtained using the getNetCapabilities method to get bearerTypes. When the value of bearerTypes is 4, it indicates that a VPN is used. Sample code:

connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
  connection.getNetCapabilities(netHandle).then((data: connection.NetCapabilities) => {
    const type: Number = data.bearerTypes[0];
    if (type === 4) {
      console.info('is vpn');
    }
  })
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)