# network

In the Mini Program/Small games in the use of network related API The following questions should be noted, and developers should know in advance.

# 1. Server Domain Name Configuration

Each WeChat Mini Program needs to set the communication domain name in advance.**You can only communicate with the specified domain name.**Including ordinary HTTPS Request (wx.request), Upload Files (wx.uploadFile), download the file (wx.downloadFile) and WebSocket Communications (wx.connectSocket)。

From the base library 2.4.0 Start, the network interface allows communication with the LAN IP Communication, but be aware that Not allowed with the native IP Signal communication

from 2.7.0 Started, provided the UDP Communications (wx.createUDPSocket)。

from 2.18.0 Started, provided the TCP Connection (wx.createTCPSocket), only allowed to communicate with non-native computers within the same LAN IP And configured server domain communications.

Such as the use of[WeChat cloud hosting](https://cloud.weixin.qq.com/cloudrun?utm _source=wxdoc&utm_content=network)As a back-end service, you do not need to configure the communication domain name (in the Mini Program through the[callContainer](https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun /src/development/call/Mini.html)and[connectContainer](https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun /src/development/WebSocket /miniprogram.html)Initiate cloud hosting service via WeChat private agreement HTTPS Call and WebSocket Communications).

# Configuration process

The server domain name is available at Mini Program background - development - development settings - server domain name You need to pay attention when configuring:

  • Domain name only. https (wx.requestwx.uploadFilewx.downloadFile) and wss (wx.connectSocket) agreement
  • Domain name cannot be used IP Address (the Mini Program'slocal area network IP Excluded) or localhost
  • for https Domain name, ports can be configured such as https://Myserver.com: 8080, but can only be configured to https://myserver.com:8080 Initiate the request. If to https://myserver.com、https://myserver.com:9091 etc. URL The request will fail. If the port is not configured. Such as https://Myserver.com, then the requested URL You can't include ports in even the default 443 The port does not either. If to https://myserver.com:443 The request will fail.
  • for wss Domain name, no need to configure the port, the default allows to request all ports under the domain name.
  • The domain name must go through ICP Keep on record
  • For security reasons,api.weixin.qq .com Cannot be configured as a server domain name, and the associated API cannot be called within the Mini Program. The developer should AppSecret Saved to a backend server, through the server using the getAccessToken Interface acquisition access_Token, and call the associated API
  • Does not support configuring parent domain names, use child domain names.

# 2. DNS Preresolved Domain Names

WeChat Client iOS 8.0.24,Android 8.0.23) Start Support.

Mini Programs generally rely on some network requests (such as logical layer wx.request, rendering layer images and other network resources), optimizing the request speed will improve the user experience, and network request time includes DNS resolution. DNS pre-resolved domain name, is a framework to provide a Mini ProgramWhen started, technology for resolving business domain names in advance.

# Configuration process

DNS Domain Name Configuration RequestMini Program background - development - development settings - server domain name You need to pay attention when configuring:

  • Pre-parsing domain names do not need to fill in the agreement header
  • Pre-resolved domain names can be added up to 5 individual
  • Other security policies are the same as the server domain name configuration policy

# 3. Network request

# Timeout

  • The default timeout is 60s
  • The timeout period can be found in the app.json or game.json Adopted in networktimeout To configure
  • You can also specify a timeout when an interface is invoked, such as wx.request({ timeout: 5000 }), in units of ms. The interface calls thetimeoutConfiguration priority is higher thanapp.jsonConfiguration in

# Use of the Site

  • Network Request Referer header Cannot be set. Its format is fixed https://servicewechat.com /{appid} /{version}/page-frame.html, which {appid} For the Mini Program. Appid, {version} Is the version number of the Mini Program, which is 0 Represented as the Development, Experience, and Audit versions, the version number is Devtools Expressed as a developer tool and the rest as an official release
  • wx.requestwx.uploadFilewx.downloadFile The maximum concurrency limit for 10 individual
  • wx.connectSocket The maximum concurrency limit for 5 A.
  • After the Mini Program runs in the background, if 5s The internal network request has not ended, an error message will be called back fail interruptedThe network request interface call cannot be called until it returns to the foreground.

# Return value encoding

  • It is recommended that the server return value use the UTF-8 Coding. For non UTF-8 Encoding, the Mini Program will try to convert, but there is a possibility that the conversion will fail.
  • The Mini Program will automatically BOM Header is filtered (only one BOM header is filtered).

# callback

  • As long as the server return is successfully received, regardless of statusCode How much? They're all going in. success Callback. Ask the developer to judge the return value according to the business logic.

# 4. Common problem

# HTTPS certificate

Small programs must be used HTTPS/WSS Initiate a network requestThe domain name used by the server at the time of the request HTTPS Certificate is verified, and if the verification fails, the request cannot be successfully initiated. Due to system limitations, different platforms have different requirements for certificates. To ensure compatibility of Mini Programs, developers are advised to configure certificates to the highest standards and use relevant tools to check whether existing certificates meet the requirements.

The certificate requirements are as follows:

  • HTTPS Certificate must be valid
    • The certificate must be trusted by the system, i.e. the root certificate has been built into the system
    • deploy SSL The website domain name of the certificate must match the domain name from which the certificate was issued
    • The certificate must be valid within the
    • The trust chain for the certificate must be complete (requires server configuration)
  • iOS Self-signed certificates are not supported
  • iOS The certificate must satisfy the Apple App Transport Security (ATS) The requirements of
  • TLS Must support 1.2 And above versions. Partly Old Android Models are not supported TLS 1.2. Please ensure that HTTPS The server's TLS Version support 1.2 And the following versions
  • part CA It may not be trusted by the operating system. Developers should pay attention to the relevant announcements of Mini programs and various systems when selecting certificates.

Certificate validity can be used openssl s_client -connect example.com:443 Command validation, you can also use other[Online tools](https://myssl.com /ssl.html)。

In addition to network requests API Other Mini programs. HTTPS Request if there is an exception, please also follow the above process to check. Such as https The picture cannot be loaded, audio and video cannot be played, etc.

# Skip domain name verification

In the WeChat developer tool, you can temporarily open The development environment does not verify the requested domain name, TLS version, and HTTPS certificate Option to skip validation of the server domain name. At this time, the verification of the server domain name will not be carried out in the WeChat developer tool and when the mobile phone is in debugging mode.

After the server domain name configuration is successful, it is recommended that developers turn off this option for development and test it on various platforms to confirm that the server domain name configuration is correct.

If it appears on the phone "Turn on debug mode to make a request, turn off debug mode to make a request" Please confirm whether the domain name verification was skipped, and confirm whether the server domain name and certificate configuration are correct.

# Overseas Users Request Acceleration

For overseas users, it can be speeded up by deploying access points overseas as well. Tencent Cloud Global Application Acceleration Service Or other similar products.