Golang HTTPS / TLS POST客户端/服务器

I have written a simple client/server in Go that will do an HTTP GET over TLS, but I'm trying to also make it capable of doing an HTTP POST over TLS.

In the example below index.html just contains the text hello, and the HTTP GET is working fine. I want the client to get the HTTP GET and write back, hello world to the server.

client

package main

import (
    "crypto/tls"
    "fmt"
    "io/ioutil"
    "net/http"
    "strings"
)

func main() {
    link := "https://10.0.0.1/static/index.html"

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    response, err := client.Get(link)
    if err != nil {
        fmt.Println(err)
    }
    defer response.Body.Close()

    content, _ := ioutil.ReadAll(response.Body)
    s := strings.TrimSpace(string(content))

    fmt.Println(s)

    // out := s + " world"      
    // Not working POST...
    // resp, err := client.Post(link, "text/plain", &out)

}

server

package main

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

func main() {
    http.HandleFunc("/static/", func (w http.ResponseWriter, r *http.Request) {
        fmt.Println("Got connection!")
        http.ServeFile(w, r, r.URL.Path[1:])
    })
    log.Fatal(http.ListenAndServeTLS(":443", "server.crt", "server.key", nil))
}

I also currently have nothing to handle the POST on the server side, but I just want it to print it out to the screen so when I run the client I will see the server print hello world.

How should I fix my client code to do a proper POST? And what should the corresponding server code look like to accept the POST? Any help would be appreciated, I'm having trouble finding HTTPS/TLS POST examples.

dongxing7318
dongxing7318 如果您提到您当前的POST尝试不起作用,这将很有用。
接近 4 年之前 回复
douyi9597
douyi9597 我使用的是自签名证书...与我的问题无关。
接近 4 年之前 回复
dqqn32019
dqqn32019 您需要将证书的CA添加到您的传输中,例如
接近 4 年之前 回复

1个回答



您没有共享错误消息,但是我认为 client.Post </ code>调用不允许 字符串作为第三个参数,因为它需要 io.Reader </ code>。 改试试这个:</ p>

  out:= s +“ world” 
resp,err:= client.Post(链接,“ text / plain”,bytes.NewBufferString(out) )
</ code> </ pre>

在服务器端,您已经设置了正确的代码来处理POST请求。 只需检查以下方法:</ p>

  http.HandleFunc(“ / static /”,func(w http.ResponseWriter,r * http.Request){
如果r.Method = =“ POST” {
//处理POST请求
}否则{
//处理所有其他请求
}
})
</ code> </ pre>

I 注意到另一个问题。 使用 index.html </ code>在这里可能行不通。 http.ServeFile </ code>将重定向该路径。 请参见 https://golang.org/pkg/net/http/#ServeFile : </ p>


在特殊情况下,ServeFile会将r.URL.Path
以“ /index.html”结尾的任何请求重定向到同一路径,而没有最终的\ n“ index.html”。 为避免此类重定向,请修改路径或使用
ServeContent。</ p>
</ blockquote>

我建议您仅使用其他文件名来避免该问题。</ p>

</ div>

展开原文

原文

You didn't share the error message, but I assume the client.Post call wasn't allowing a string as its third parameter, because it requires an io.Reader. Try this instead:

out := s + " world"     
resp, err := client.Post(link, "text/plain", bytes.NewBufferString(out))

On the server side, you already have the right code set up to handle the POST request. Just check the method:

http.HandleFunc("/static/", func (w http.ResponseWriter, r *http.Request) {
    if r.Method == "POST" {
        // handle POST requests
    } else {
        // handle all other requests
    }
})

I noticed one other issue. Using index.html probably won't work here. http.ServeFile will redirect that path. See https://golang.org/pkg/net/http/#ServeFile:

As a special case, ServeFile redirects any request where r.URL.Path ends in "/index.html" to the same path, without the final "index.html". To avoid such redirects either modify the path or use ServeContent.

I'd suggest just using a different file name to avoid that issue.

dongxieting9623
dongxieting9623 非常感谢你!
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐