用Go获取短信验证码的示例代码

  // 平台注册地址 vip.veesing.com

  package main

  import (

  "fmt"

  "strings"

  "net/http"

  "io/ioutil"

  )

  func main() {

  url := "https://vip.veesing.com/smsApi/verifyCode"

  method := "POST"

  // 替换示例代码中的"YOUR_APP_ID"、"YOUR_APP_KEY"、"YOUR_TEMPLATE_ID"、"YOUR_PHONE"、"YOUR_CODE"为你在中昱维信账号中获得的实际值

  payload := strings.NewReader("appId=YOUR_APP_ID&appKey=YOUR_APP_KEY&templateId=YOUR_TEMPLATE_ID&phone=YOUR_PHONE&variables=YOUR_CODE")

  client := &http.Client {

  }

  req, err := http.NewRequest(method, url, payload)

  if err != nil {

  fmt.Println(err)

  return

  }

  req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

  res, err := client.Do(req)

  if err != nil {

  fmt.Println(err)

  return

  }

  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)

  if err != nil {

  fmt.Println(err)

  return

  }

  // 解析短信服务的响应response,根据返回结果判断是否发送成功

  // 成功 {"returnStatus": "1 ", "message": "成功", "remainPoint": "241", "taskId": "3313746", "successCounts": "1"}

  // 失败 {"returnStatus": "0", "message": "参数错误", "remainPoint": null, "taskId": null, "successCounts": null}

  // 处理成功或失败的逻辑...

  fmt.Println(string(body))

  }