DEV Community

魔眼天王
魔眼天王

Posted on

Comprehensive Guide to Network Requests in HarmonyOS: From Permission Configuration to Implementation

I. Network Permission Architecture

1. Permission Configuration Specifications

// module.json5 configuration example "requestPermissions": [ { "name": "ohos.permission.INTERNET", // Core network permission "reason": "Access to cloud services required", "usedScene": "Data synchronization scenarios" }, { "name": "ohos.permission.ACCESS_WIFI_STATE", // WiFi state permission "reason": "Optimize network selection strategy" } ] 
Enter fullscreen mode Exit fullscreen mode

Permission hierarchy mechanism:

  • ​System Pre-Grant: INTERNET permission granted by default (system_grant)

  • ​Dynamic Grant: User-confirmation required permissions (user_grant), e.g., location access

II. Official Network Request Solutions

1. Native HTTP Module

Implementation Steps:

// 1. Create request object import http from '@ohos.net.http'; let httpRequest = http.createHttp(); // 2. Configure request parameters const options = { method: http.RequestMethod.GET, url: "https://api.example.com/data", header: { "Content-Type": "application/json" }, connectTimeout: 10000, readTimeout: 15000 } // 3. Send request httpRequest.request(options, (err, data) => { if(!err) { console.log(JSON.parse(data.result as string)); } }); // 4. Resource cleanup httpRequest.destroy(); 
Enter fullscreen mode Exit fullscreen mode

Core Features:

  • Protocol support: HTTP/1.1 (including WebSocket)

  • Connection management: Automatic TCP connection reuse

  • Caching strategy: Memory + disk two-tier caching

2. Axios Porting Solution

# Install axios ohpm i @ohos/axios 
Enter fullscreen mode Exit fullscreen mode
import axios from '@ohos/axios'; // Create instance const instance = axios.create({ baseURL: 'https://api.example.com', timeout: 5000 }) // Interceptor configuration instance.interceptors.request.use(config => { config.headers.Authorization = `Bearer ${token}`; return config; }); 
Enter fullscreen mode Exit fullscreen mode

Feature Comparison:

Feature Native HTTP Axios
Type Support Manual parsing required Automatic JSON conversion
Interceptors Custom implementation needed Built-in interceptors
Error Handling Callback functions Promise chaining
File Upload Manual handling FormData support

III. Underlying Implementation Principles

1. Network Stack Architecture

Application Layer └── Network APIs (HTTP/WebSocket) └── Network Service Framework (NetService) └── Network Protocol Stack (TCP/IP) └── Hardware Abstraction Layer (Network Drivers) 
Enter fullscreen mode Exit fullscreen mode

2. Key Technical Components

  • ​Connection Pool Management: Maintains 4 persistent connections by default

  • ​DNS Resolution: Local cache + system-level resolution

  • ​Certificate Validation: System CA certificate storage

  • ​Traffic Monitoring: NetworkKit API integration

IV. Advanced Implementation Scenarios

1. WebSocket Communication

import { WebSocket } from '@ohos.net.websocket'; const ws = new WebSocket('wss://echo.websocket.org'); ws.onopen = () => { ws.send('Hello Server'); } ws.onmessage = (event) => { console.log(event.data); } 
Enter fullscreen mode Exit fullscreen mode

2. Resumable File Upload

const uploadFile = async (filePath: string) => { const file = await fileio.open(filePath, fileio.OpenMode.READ); const totalSize = await fileio.stat(filePath).then(stat => stat.size); let uploaded = 0; while(uploaded < totalSize) { const chunk = await file.read(totalSize - uploaded, 1024 * 1024); await http.post('https://api/upload', { header: { 'Content-Range': `bytes ${uploaded}-${uploaded+1023}` } }, chunk); uploaded += 1024 * 1024; } } 
Enter fullscreen mode Exit fullscreen mode

V. Performance Optimization Strategies

1. Connection Reuse Configuration

// In ApplicationAbility class MyAbility extends Ability { onInit() { const config = { maxConnectionsPerRoute: 6, connectionTimeout: 15000, socketOptions: { soKeepAlive: true, tcpNoDelay: true } }; NetworkManager.getInstance().setGlobalConfig(config); } } 
Enter fullscreen mode Exit fullscreen mode

2. Caching Optimization

const cacheConfig = { policy: CachePolicy.CACHE_FIRST, maxAge: 3600, // 1 hour cache storage: { path: '/data/cache', maxSize: 1024 * 1024 * 50 // 50MB } } http.setRequestCacheConfig(cacheConfig); 
Enter fullscreen mode Exit fullscreen mode

VI. Debugging & Monitoring

1. Network Sniffing

# Start packet capture deveco-cli network-sniffer --port=8080 
Enter fullscreen mode Exit fullscreen mode

2. Performance Metrics

Metric Target Monitoring Method
DNS Resolution Time <100ms NetworkProfiler
TCP Handshake Latency <200ms DevTools Network Panel
TTFB (Time to First Byte) <500ms Performance API
Total Request Time <1500ms Resource Timing API

VII. Implementation Recommendations

  1. ​Connection Lifecycle Management:
    Always call destroy() to release resources promptly

  2. ​Retry Mechanism Design:
    Implement exponential backoff strategy (maxRetries=3)

  3. ​Security Enhancements:

 // Enforce HTTPS const secureOptions = { httpsOnly: true, certificatePinner: { pins: ['sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='] } } 
Enter fullscreen mode Exit fullscreen mode
  1. ​Multi-environment Configuration:
 const env = process.env.NODE_ENV; const baseURL = env === 'production' ? 'https://api.prod.com' : 'https://api.dev.com'; 
Enter fullscreen mode Exit fullscreen mode

This comprehensive guide provides a complete framework for building reliable network modules in HarmonyOS. Developers are recommended to utilize DevEco Studio's Network Profiler for real-time performance optimization and consider hybrid solutions combining Axios with native modules for complex scenarios.

Top comments (0)