我正在尝试通过 GOLang 中的 TCP/TLS 套接字发送 HTTP 请求,但是当我尝试发送它时,无论主机是什么,它总是返回(错误请求 400),所以我认为它一定有问题我用来通过 TCP/TLS 发送请求的方式,我进行了搜索,并寻找了一种方法来做我想做的事情,但没有结果:
package main
import (
"crypto/rand"
"crypto/tls"
"fmt"
"net"
"strings"
)
func main() {
headers := make(map[string]string)
headers["Accept"] = "application/json"
request := parseRequest("httpbin.org", "/get", "get", headers, nil, "")
println(request)
tcpConn, err := net.Dial("tcp", "httpbin.org:443")
if err != nil {
panic(err)
}
cf := &tls.Config{Rand: rand.Reader, InsecureSkipVerify: true}
ssl := tls.Client(tcpConn, cf)
if err != nil {
panic(err)
}
defer ssl.Close()
n, err := ssl.Write([]byte(request))
if err != nil {
fmt.Println(n, err)
return
}
buf := make([]byte, 1024)
n, err = ssl.Read(buf)
if err != nil {
fmt.Println(n, err)
return
}
println(string(buf[:n]))
}
func parseRequest(host string, path string, method string, headers map[string]string, data map[string]string, rawData string) string {
method = strings.ToUpper(method)
rawRequest := fmt.Sprintf(`%s %s HTTP/1.1\r\n`, strings.ToUpper(method), path)
if method == `GET` && (data != nil || rawData != ``) {
var rawQuery string
if rawData != `` {
rawQuery = rawData
} else {
i := 0
for key, value := range data {
if i == len(data)-1 {
rawQuery += fmt.Sprintf(`%s=%s`, key, value)
} else {
rawQuery += fmt.Sprintf(`%s=%s&`, key, value)
}
i++
}
}
query := fmt.Sprintf(`%s?%s`, path, rawQuery)
rawRequest = fmt.Sprintf(`%s %s HTTP/1.1\r\n`, strings.ToUpper(method), query)
}
var rawHeaders string
for key, value := range headers {
rawHeaders += fmt.Sprintf(`%s: %s\r\n`, key, value)
}
if !strings.Contains(strings.ToLower(rawHeaders), `host`) {
rawRequest += fmt.Sprintf(`Host: %s\r\n`, host)
}
rawRequest += fmt.Sprintf(`%s\r\n`, rawHeaders)
if method == `POST` && (data != nil || rawData != ``) {
jeck猫
相关分类