目录

Network Access

Configuring a Server Domain Name

To provide a secure and reliable network environment for users and improve their trust and satisfaction, you have to configure the domain name of the target server. For details, see Configuring Server Domain Names.

During atomic service development, you can temporarily turn on Waive restrictions for services under development on HarmonyOS devices to skip the verification of the server domain name.

Requesting HTTP Data

You can start a data request using has.request. Common methods include GET, POST, OPTIONS, HEAD, PUT, DELETE, TRACE, and CONNECT.

Before using this function, you need to configure the server domain name.

has.request({
  url: 'https://www.example.com', // Replace the example URL with the actual URL.
  data: {
    x: '',
    y: ''
  },
  success: (res) => {
    console.info('request success', res.data);
  },
  fail: (err) => {
    console.error('request fail', err);
  },
  complete: (res) => {
    console.info('request complete', res);
  }
});

Establishing a WebSocket Connection

Create a WebSocket connection using has.connectSocket to send and receive data.

has.connectSocket({
  url: 'ws://www.example.com', // Replace the example URL with the actual URL.
  header: {
    'content-type': 'application/json'
  },
  protocols: ['protocol1'],
  success: () => {
    console.info('connectSocket success');
  },
  fail: (err) => {
    console.error('connectSocket fail', err);
  },
  complete: (res) => {
    console.info('connectSocket complete', res);
  }
});