将下面代码改为GO代码host = 'https://kuaidi100.market.alicloudapi.com' path = '/getExpress' method = 'GET' appcode = '你自己的AppCode' querys = 'NO=12345678&TYPE=yuantong' bodys = {} url = host + path + '?' + querys request = urllib2.Request(url) request.add_header('Authorization', 'APPCODE ' + appcode) ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE response = urllib2.urlopen(request, context=ctx) content = response.read() if (content): print(content)
时间: 2024-03-28 17:40:09 浏览: 114
以下是将代码改为GO代码的示例:
```go
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
host := "https://kuaidi100.market.alicloudapi.com"
path := "/getExpress"
method := "GET"
appcode := "你自己的AppCode"
querys := "NO=12345678&TYPE=yuantong"
url := host + path + "?" + querys
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Authorization", "APPCODE " + appcode)
res, err := http.DefaultClient.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
}
fmt.Println(string(body))
}
```
请注意,由于GO语言的标准库中已经包含了HTTP客户端,因此我们不需要像Python中一样使用`urllib2`和`ssl`模块,而是直接使用GO语言的`http`包发送HTTP请求,并使用`ioutil`包读取响应内容。
阅读全文