我正在尝试使用 golang 从服务器获取响应http
客户。
我希望通过 go 执行的请求应与以下内容相同curl
命令:
curl --data "fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142" 'http://www.homefacts.com/hfreport.html'
我已经编写了等效的 go 代码,并且还尝试使用名为卷曲即走 https://mholt.github.io/curl-to-go/,它为上面生成以下 go 代码curl
要求:
// Generated by curl-to-Go: https://mholt.github.io/curl-to-go
body := strings.NewReader(`fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142`)
req, err := http.NewRequest("POST", "http://www.homefacts.com/hfreport.html", body)
if err != nil {
// handle err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
// handle err
}
defer resp.Body.Close()
问题是我不断得到不同的回应curl
命令和 go 代码。这curl
命令返回此响应正文:
<html><head><meta http-equiv="refresh" content="0;url=http://www.homefacts.com/address/Arizona/Maricopa-County/Queen-Creek/85142/22280-S-209th-Way.html"/></head></html>
这是预期的结果。然而 go 代码返回一个很长的HTML
这不是预期的结果。
我尝试过添加--verbose
to the curl
命令复制其所有标头,因此我通过 go 代码添加了以下标头:
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "curl/7.51.0")
req.Header.Set("Accept", "*/*")
req.Header.Set("Content-Length", "56")
但仍然不高兴,go 代码的输出仍然与curl
one.
关于如何获得相同的任何想法curl
来自 go 的响应?