首页 > 解决方案 > 简单的 HTTPS 请求:Golang 返回 505,Python 和 Chrome 工作

问题描述

我正在尝试使用最简单的可以想象的 golang 代码通过 TLS 执行 HTTP 获取,并从服务器获取 505 响应(不支持 HTTP 版本)。问题是,使用简单的 python requests.get 相同的查询可以工作。此外,我可以使用 Chrome 并成功执行相同的请求。

任何想法是什么使 golang 请求不同而导致服务器返回 505?

我意识到这个响应是特定于服务器的。使用相同的 golang 代码到 google.com 的 HTTPS 有效。我尝试使用 Wireshark 进行故障排除,但 TLS 使这变得困难。看来这一定很简单!服务器是 nginx 1.9.3。

戈朗代码:

package main

import (
        "fmt"
        "net/http"
        "time"
)

func main() {
        url := "https://non-public-address/page"
        tr := &http.Transport{
                MaxIdleConns:       10,
                IdleConnTimeout:    30 * time.Second,
                DisableCompression: false,
        }
        client := &http.Client{Transport: tr}
        resp, _ := client.Get(url)
        fmt.Println(resp)
}

Python:

r = requests.get("https://non-public-address/page")
print(r)

标签: gohttps

解决方案


I figured out the issue eventually by decrypting the traffic in Wireshark. The URL in my Go implementation has spaces and using http.Get didn't URL encode the spaces to %20. With spaces in the GET request the server was misinterpreting the request.

Lesson learned: Go's http.Get does not URL encode characters and you have to do this on your own.


推荐阅读