/** * * 推荐尽量使用POST方式 * sdk接口都均以https方式调用,客户端需要验证服务器证书是否正常。 * cacert.pem为可信任根证书文件。用户请保持更新 */ package APIStore import ( "fmt" "io/ioutil" "math/rand" "net/http" "net/url" "os" "strconv" "strings" "time" ) //post访问方式(推荐) //host 请求地址 //param 附加参数 func RequestPost(host string, param string) { body := ioutil.NopCloser(strings.NewReader(param)) req, err := http.NewRequest("POST", host, body) if err != nil { fmt.Println("Fatal error ", err.Error()) os.Exit(0) } req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") req.Header.Add("Accept-Encoding", "UTF-8") req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") //这个一定要加,不加form的值post不过去 //fmt.Printf("%+v\n", req) //看下发送的结构 client := &http.Client{} resp, err := client.Do(req) //发送 if err != nil { fmt.Println("error:", err) return } defer resp.Body.Close() //一定要关闭resp.Body data, err := ioutil.ReadAll(resp.Body) } //get方式请求 //host 请求地址 //param 附加参数 func RequestGet(host string, param string) { req, err := http.NewRequest("GET", host+"?"+param, nil) if err != nil { fmt.Println("Fatal error ", err.Error()) os.Exit(0) } req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") req.Header.Add("Accept-Encoding", "UTF-8") //fmt.Printf("%+v\n", req) //看下发送的结构 client := &http.Client{} resp, err := client.Do(req) //发送 if err != nil { fmt.Println("error:", err) return } defer resp.Body.Close() //一定要关闭resp.Body data, err := ioutil.ReadAll(resp.Body) } //获取内容 func getContent(requestType string) { var url string="https://v.1dq.com/api/c43"; var param string ="key=您申请的key&bankcard=123456789&apiversion=2.0.5"; if requestType == "GET" { RequestGet(url, param) } else { RequestPost(url, param) } } }