各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《当其他语言和curl成功时,无法发送Go请求(EOF或“对等方重置连接”)》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!
问题内容我在使用 golang 时遇到一个请求问题。
它总是返回错误eof或“连接被对等方重置”。
但是,当我使用其他语言(例如 python)(使用包“requests”)和 curl 时,它会成功返回响应。
这里我有这些请求的示例代码和相应的curl命令(我已经更改了cookie等敏感信息):
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
var (
URL = "https://p4-msg.chat.zalo.me/?zpw_ver=49&zpw_type=30¶ms=1456344318999,1456344318999,eyIxIjp7Imxhc3RJZCI6IjE0NTYzNDQzMTg5OTkiLCJpZHMiOlsiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiXX0sIjIiOnsibGFzdElkIjoiMTQ1NjM0NDMxODk5OSIsImlkcyI6W119LCIzIjp7Imxhc3RJZCI6IjE0NTYzNDQzMTg5OTkiLCJpZHMiOlsiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiLCIxNDU2MzQ0MzE4OTk5IiwiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiLCIxNDU2MzQ0MzE4OTk5IiwiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiLCIxNDU2MzQ0MzE4OTk5Il19fQ%3D%3D%2C1&ts=1587720743"
Cookie = "__zi=3000.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.1; __zi-legacy=3000.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.1; fpsend=333333; zpsid=AAAA.333.3.1; zpsidleg=AAAA.333.3.1; zpw_sekm=AAAA.333.3.2; app.event.zalo.me=3333333333333333333; _zlang=vn; zpw_sek=AAAA.333.a0.F-AAAAAAAAAAAAAAAAAAAA-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABB"
Headers = map[string]string{
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:75.0) Gecko/20100101 Firefox/75.0",
"Accept": "application/json, text/plain",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "keep-alive",
//"Cookie": Cookie,
}
)
func main() {
curl := fmt.Sprintf(`curl "%s"`, URL)
client := http.DefaultClient
request, err := http.NewRequest("GET", URL, nil)
if err != nil {
fmt.Println("new request error", err.Error())
return
}
for k, v := range Headers {
curl = fmt.Sprintf("%s -H '%s: %s'", curl, k, v)
request.Header.Set(k, v)
}
for _, v := range strings.Split(Cookie, ";") {
if len(v) < 1 {
continue
}
tmp := strings.Split(v, "=")
if len(tmp) < 2 {
continue
}
request.AddCookie(&http.Cookie{
Name: tmp[0],
Value: tmp[1],
Path: "",
Domain: "p4-msg.chat.zalo.me",
Expires: time.Time{},
RawExpires: "",
MaxAge: 0,
Secure: false,
HttpOnly: false,
SameSite: 0,
Raw: "",
Unparsed: nil,
})
}
fmt.Println(curl)
resp, err := client.Do(request)
if err != nil {
fmt.Println("request error", err.Error())
return
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("read body error", err.Error())
return
}
fmt.Printf("response: %s\n", content)
}
编辑: 我添加了 addcookie 部分。结果还是一样。
解决方案
正如 @adrian 提到的,你应该这样做:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
var (
URL = "https://p4-msg.chat.zalo.me/?zpw_ver=49&zpw_type=30¶ms=1456344318999,1456344318999,eyIxIjp7Imxhc3RJZCI6IjE0NTYzNDQzMTg5OTkiLCJpZHMiOlsiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiXX0sIjIiOnsibGFzdElkIjoiMTQ1NjM0NDMxODk5OSIsImlkcyI6W119LCIzIjp7Imxhc3RJZCI6IjE0NTYzNDQzMTg5OTkiLCJpZHMiOlsiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiLCIxNDU2MzQ0MzE4OTk5IiwiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiLCIxNDU2MzQ0MzE4OTk5IiwiMTQ1NjM0NDMxODk5OSIsIjE0NTYzNDQzMTg5OTkiLCIxNDU2MzQ0MzE4OTk5Il19fQ%3D%3D%2C1&ts=1587720743"
Cookie = "__zi=3000.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.1; __zi-legacy=3000.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.1; fpsend=333333; zpsid=AAAA.333.3.1; zpsidleg=AAAA.333.3.1; zpw_sekm=AAAA.333.3.2; app.event.zalo.me=3333333333333333333; _zlang=vn; zpw_sek=AAAA.333.a0.F-AAAAAAAAAAAAAAAAAAAA-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABB"
Headers = map[string]string{
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:75.0) Gecko/20100101 Firefox/75.0",
"Accept": "application/json, text/plain",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "keep-alive",
}
)
func main() {
curl := fmt.Sprintf(`curl "%s"`, URL)
client := http.DefaultClient
request, err := http.NewRequest("GET", URL, nil)
if err != nil {
fmt.Println("new request error", err.Error())
return
}
for k, v := range Headers {
curl = fmt.Sprintf("%s -H '%s: %s'", curl, k, v)
request.Header.Set(k, v)
}
request.AddCookie(&http.Cookie{
Raw: Cookie,
})
fmt.Println(curl)
resp, err := client.Do(request)
if err != nil {
fmt.Println("request error", err.Error())
return
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("read body error", err.Error())
return
}
fmt.Printf("response: %s\n", content)
}
本篇关于《当其他语言和curl成功时,无法发送Go请求(EOF或“对等方重置连接”)》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注编程网公众号!