LicenseManger/examples/client.go

75 lines
1.4 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
ServerURL = "http://localhost:8080"
DeviceUID = "test-device-001"
)
func main() {
// 1. 使用授权码注册设备
if err := registerDevice(); err != nil {
fmt.Printf("设备注册失败: %v\n", err)
return
}
// 2. 验证设备状态
if err := validateDevice(); err != nil {
fmt.Printf("设备验证失败: %v\n", err)
return
}
fmt.Println("设备注册和验证成功")
}
func registerDevice() error {
data := map[string]interface{}{
"uid": DeviceUID,
"device_type": "software",
"device_model": "test-model",
"company": "test-company",
"license_code": "your-license-code", // 替换为实际的授权码
}
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
resp, err := http.Post(ServerURL+"/api/devices/register",
"application/json", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("注册失败: %s", string(body))
}
return nil
}
func validateDevice() error {
resp, err := http.Get(ServerURL + "/api/devices/validate/" + DeviceUID)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("验证失败: %s", string(body))
}
return nil
}