go - 简单的 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)
解决方案
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.
推荐阅读
- excel - 查找某个文本,将文本下方的所有内容复制到另一个工作簿中的相同文本下
- python-3.x - 如何根据ggplot中的数据集按小时绘制活动?
- java - 杰克逊日期从 JSON 文件反序列化错误
- vue.js - 如何避免用户单击另一个编辑按钮到另一行?
- php - 我拥有的代码不会在数据库中创建值
- flutter - 如何在 DartLang 中封装一个 Map?
- reactjs - 在 reactjs 中创建多个 firebase 实例
- android - Ionic V4 应用程序无法在 android 4.4.2 上运行,但可以运行
- r - R中单个长命令(不是循环)的进度条
- python - 如何调整热图单元格的高度/宽度?