我坚持使用Golang执行get请求,我也尝试了三种不同的实现,但没有成功。对于他们所有人我收到此错误消息:
获取https://11.11.11.1:0000/httpgw.conf?Type=SMS&Address=12345678&MsgID=12 3& Notify = N& Validity = 24:00& OAdC = 15555& Message = HelloBrother:tls:超大型录音机 d收到长度为20527
Bellow是我正在处理的完整源代码:
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
cmdSecSMS := "https://11.11.11.1:0000/httpgw.conf?Type=SMS&Address=12345678&MsgID=123&Notify=N&Validity=24:00&OAdC=15555&Message="
msg := "HelloBrother"
cmdSecUrlSMS := cmdSecSMS + msg
doClientTrans(cmdSecUrlSMS)
doGetClient(cmdSecUrlSMS)
doGet(cmdSecUrlSMS)
}
func doClientTrans(address string) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
response, err := client.Get(address)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(contents))
fmt.Println(" Size: ", len(string(contents)), " url: ", address)
fmt.Println(" Status Code: ", response.StatusCode)
hdr := response.Header
for key, value := range hdr {
fmt.Println(" ", key, ":", value)
}
}
}
func doGet(url string) {
response, err := http.Get(url)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Println(" Size: ", len(string(contents)), " url: ", url)
fmt.Println(" Status Code: ", response.StatusCode)
hdr := response.Header
for key, value := range hdr {
fmt.Println(" ", key, ":", value)
}
}
}
func doGetClient(url string) {
client := &http.Client{}
response, err := client.Get(url)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Println(" Size: ", len(string(contents)), " url: ", url)
fmt.Println(" Status Code: ", response.StatusCode)
hdr := response.Header
for key, value := range hdr {
fmt.Println(" ", key, ":", value)
}
}
}
使用telnet时,此GET请求正常工作:
telnet 11.11.11.1 0000
获取https://11.11.11.1:0000/httpgw.conf?Type=SMS&Address=12345678&MsgID=12 3& Notify = N& Validity = 24:00& OAdC = 15555& Message = HelloBrother HTTP / 1.1
^: 退出
我在Windows Server 2012中运行golang应用程序,而且我对服务器技术堆栈一无所知。
可以解决这个问题吗?我可以尝试一种配置解决方法或其他方法吗?
感谢您的帮助
答案 0 :(得分:1)
我很高兴与您分享我达到了所需的实施方式。
下面的代码有效!
感谢您的意见,指导我采用正确的方法完成此任务:
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
)
func main() {
cmdSecSMS := "GET https://10.xxx.xx.x:xx43/httpgw.conf?" +
"Type=SMS&Address=5511111&MsgID=123&Notify=N&Validity=24:00&OAdC=15555&" +
"Message=blablah " +
"HTTP/1.1"
fmt.Println(cmdSecSMS)
cmdSecUrlSMS := cmdSecSMS
hostName := "10.xxx.xx.x"
portNum := "xx43"
doDial(cmdSecUrlSMS, hostName, portNum)
//doClientTrans(cmdSecUrlSMS)
//doGetClient(cmdSecUrlSMS)
//doGet(cmdSecUrlSMS)
}
func doDial(cmd, host, port string) {
// connect to this socket
conn, err := net.Dial("tcp", host+":"+port)
if err != nil {
fmt.Printf("Some error %v", err)
return
} else {
defer conn.Close()
fmt.Printf("Connection established between %s and localhost.\n", host)
fmt.Printf("Local Address : %s \n", conn.LocalAddr().String())
fmt.Printf("Remote Address : %s \n", conn.RemoteAddr().String())
// send to socket
fmt.Fprintf(conn, cmd+"\n")
// listen for reply
message, _ := bufio.NewReader(conn).ReadString('\n')
fmt.Print("Message from server: " + message)
}
}
谢谢你们的支持!