# network
When using network-related APIs in Weixin Mini Program / MiniGame, developers should be aware of the following issues in advance.
# 1. Server Domain Name Configuration
Each Weixin Mini Program needs to set up a communication domain name in advance, and Weixin Mini Program can communicate networkally only with a specified domain name .This includes a normal HTTPS request ( wx.request ), an upload file ( wx.uploadFile ),Download files ( wx.downloadFile ) and WebSocket communication ( wx.connectSocket ).
Starting with the base library 2.4.0, the network interface allows communication with local network IP, but note that does not allow communication with local IP .
As of 2.7.0, UDP communication is provided ( wx.createUDPSocket ).
From 2.18.0, a TCP connection is provided ( wx.createTCPSocket ),Only non-local IPs and configured server domain names within the same local network are allowed to communicate.
If you use WeChat cloud hosting as a back-end service, you don't need to configure the communication domain name (in theIn Weixin Mini Program, callContainer and connectContainer initiate HTTPS calls and WebSocket communications to the cloud hosting service via WeChat private protocols.
# The configuration process
Server domain name in the "Weixin Mini Program background - development - development settings - server domain name" configuration, configuration should pay attention to:
- The domain only supports [[]]
https( wx.request 、 wx.uploadFile 、 wx.downloadFile ) Andwss( wx.connectSocket ) protocol; - Domain names cannot be used with an IP address (other than [) or localhost;
- For
httpsdomains, you can configure a port such as https://myserver.com:8080, but you can only make requests to https://myserver.com: 8080 when configured.If you go to. https://myserver.com、https://myserver.com:9091The URL request will fail.If you do not configure the port, Such as https://myserver.com , then the requested URL cannot contain a port, even the default port 443.A request to https://myserver.com:443 will fail. - For the
wssdomain, there is no need to configure ports, and all ports under the domain are allowed by default. - The domain name must be registered by ICP;
- For security reasons,
api.weixin.qq.comcannot be configured as a server domain, and the related API cannot be invoked within Weixin Mini Program. The developer should save the AppSecret to the backend server, which will use thegetAccessTokeninterface to get theaccess_token, and invoke the relevant API; - Configuration of a parent domain is not supported, using a subdomain.
# 2. DNS Preresolved Domain Names
WeChat Guest House iOS 8.0.24, Android 8.0.23) started support.
Weixin Mini Program generally relies on some network requests (such as the logical layer'swx.request, the rendering layer's pictures, etc.), and optimizing the request speed will improve the user experience, and DNS resolution is a component of network request time. DNS pre-resolved domain name, is the framework provides a Mini Program start , in advance of the resolution of business domain name technology.
# The configuration process
DNS domain name configuration request "Weixin Mini Program background - development - development settings - server domain name" configuration, configuration should pay attention to:
- Pre-parse domain name does not need to fill in protocol headers
- Pre-parse domain names can add up to 5
- Other security policies and server domain name configuration policies
# 3. Network requests
# Overtime Time
- Default timeout is 60s ;
- The timeout can be found in
apagejsonorgame.jsonbynetworktimeout - You can also specify a timeout when the interface is invoked, such as
wx.request ({timeout: 5000})in ms.Thetimeoutconfiguration of the interface call is superior to the configuration inapagejson
# Restrictions on Use
- The
refererheader of the network request cannot be set. The format is fixed ashttps://servicewechat.com/{appid}/{version}/page-frame.html, where [[ TAG-2-START]] {AppID}For Weixin Mini Program appid,{version}The version number of the Mini Program, the version number is0represents the development, experience, and audit version, the version name isdevtools`represented the developer tools, and the rest the official version; - wx.request 、 wx.uploadFile 、 wx.downloadFile The maximum concurrency limit is 10 ;
- The maximum concurrency limit for wx.connectSocket is 5 .
- Weixin Mini Program After running in the background, if 5s network request is not finished, it will call back the error message
fail interrupted;Network request interface calls cannot be called until you return to the front desk.
# Return value encoding
- It is recommended to use UTF-8 coding for the server return value.For non-UTF-8 encodings, Weixin Mini Program attempts to convert, but there is a chance that the conversion will fail.
- Weixin Mini Program automatically filters the BOM header (only one BOM header is filtered).
# callback
- Whenever a server return is received successfully, no matter what the
statusCodeis, there is asuccesscallback.Ask the developer to judge the return value based on business logic.
# 4. Common problem
# HTTPS certificate
Weixin Mini Program Network requests must be initiated using HTTPS / WSS . The HTTPS certificate used by the server domain name is verified when the request is made, and if the verification fails, the request cannot be successfully initiated. Due to system limitations, different platforms have different degrees of rigor in their certificate requirements. To ensure the compatibility of the Mini Program, developers are advised to apply the highest standards for certificate configuration and use tools to check that existing certificates meet the requirements.
The requirements for certificates are as follows:
- The HTTPS certificate must be valid.
- The certificate must be trusted by the system, i.e. the root certificate is built into the system
- The domain name of the website where the SSL certificate is deployed must match the domain name issued by the certificate
- Certificates must be valid
- The certificate's chain of trust must be complete (server configuration required)
IOSdoes not support self-signed certificates;Certificates under iOSmust meet the requirements of Apple App Transport Security (ATS) ;- TLS must support version 1.2 and above.Some older
Androidmodels do not support TLS 1.2 yet, please ensure that the TLS version of the HTTPS server supports 1.2 and below; - Some CA certificates might not be trusted by the operating system. Developers should pay attention to Weixin Mini Program and relevant announcements specific to each system when selecting certificates.
Certificate validity can be obtained using
openssl s_client -connect example.com: 443Command validation, or use other online tools .
In addition to the Network Request API, the Weixin Mini Program otherHTTPSrequests in Weixin Mini Program should also be checked according to the above process if there are exceptions.Such as https pictures can not load, audio and video can not be played.
# Skip Domain Verification
In the WeChat developer tools, it is possible to temporarily turn on thedevelopment environment to skip the validation of the server domain name by not validating theoption for the request domain, TLS version and HTTPS certificate.At this time, when debugging mode is turned on in WeChat developer tools and on the mobile phone, the server domain name is not verified.
After the server domain is configured successfully, developers are advised to turn off this option for development and to test across platforms to confirm that the server domain has been configured correctly.
If the "open debugging mode can make requests, shut debugging modes can't make requests," appears on your phone, make sure you skip domain validation and make sure the server domain name and certificate are configured correctly.
# Overseas user requests accelerate
For overseas users, you can speed up by also deploying access points overseas, which you can refer to access Tencent Cloud Global App Acceleration Service or other similar products.