GB2312编码转换

package main

import (
    "fmt"
    "github.com/Tang-RoseChild/mahonia"
    "net/http"
    "io/ioutil"
)

func main() {
    SendSms("176xxxx596","wozai你好,测试")
    
}
func SendSms(phone,content string){
    if phone == "" || content ==""{
        return
    }

    charName := "GB2312"
    if gb2312 := mahonia.GetCharset(charName); gb2312 == nil{
        fmt.Errorf("编码不存在!",gb2312)
        return
    }
    gb2312 := mahonia.NewEncoder(charName)
    cs := gb2312.ConvertString(content)
    url := fmt.Sprintf("http://yl.mobsms.net/send/gsend.aspx?name=xxxx&pwd=xxxx&dst=%s&msg=%s",phone,cs)
    //fmt.Println(url)
    resp, err := http.Get(url)
    if err != nil || resp.StatusCode != http.StatusOK {
        fmt.Println("错误:发送请求", err)
        return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body) //此处可增加输入过滤
    if err != nil {
        fmt.Println("错误:读取body", err)
        return
    }

    //解析返回数据
    s := gb2312.ConvertString(string(body))
    fmt.Println(s)
}

你可能感兴趣的:(GB2312编码转换)