douxiong3234 2017-10-19 18:26
浏览 89
已采纳

Golang本机HTTP客户端挂在特定的URI上

This is a small peace of code:

//resp, err := http.Get("https://alfabank.ru")
resp, err := http.Get("https://google.com")
if err != nil {
    log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    log.Fatalln(err)
}
fmt.Println(string(body))

It works fine with Google, but hangs on alfabank (try to uncomment first line and comment second). I can't figure out on which side this problem :( But, for example,

curl https://alfabank.ru

works fine.

So, I think, that the problem in Golang client.

I tried to use tcpdump and see infinite traffic between my machine and remote server.

Could you help me to figure out the problem?

UPDATE: following the advice from comments

Ok, curl sends this headers:

> GET / HTTP/1.1
> Host: alfabank.ru
> User-Agent: curl/7.54.0
> Accept: */*

So, I tried

req, err := http.NewRequest(http.MethodGet, "http://alfabank.ru", nil)
if err != nil {
    log.Fatalln(err)
}
req.Header.Add("Accept", "*/*")
req.Header.Add("User-Agent", "curl/7.54.0")

client := http.Client{}
resp, err := client.Do(req)

It doesn't solve the problem.

  • 写回答

1条回答 默认 最新

  • dongyin2643 2017-10-20 14:53
    关注

    SOLUTION:

    Thank you to JimB, problem in http2 protocol.

    So, disabling http2 helps me to solve the problem:

    http.DefaultClient.Transport = &http.Transport{
        TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路