TLS를 통해 HTTP GET을 수행하는 간단한 클라이언트/서버를 Go에 작성했지만 TLS를 통해 HTTP POST를 수행 할 수있게하려고합니다.Golang HTTPS/TLS POST 클라이언트/서버
아래 예제에서 index.html
에는 hello
텍스트 만 포함되어 있으며 HTTP GET이 정상적으로 작동합니다. 클라이언트가 HTTP GET을 받고 서버에 hello world
을 다시 기록하도록합니다.
클라이언트
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)
}
서버
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))
}
또한 현재 서버 측에서 POST를 처리 할 수있는 게 없다,하지만 난 그냥 실행하면 그렇게 화면에 인쇄 할 클라이언트는 서버 프린트 hello world
을 볼 것입니다.
올바른 POST를 수행하기 위해 클라이언트 코드를 수정해야합니까? 그리고 POST를 받아들이려면 해당 서버 코드가 어떻게되어야합니까? 어떤 도움을 주시면 감사하겠습니다. HTTPS/TLS POST 예제를 찾는 데 문제가 있습니다.
인증서에 CA를 추가해야합니다 (예 : http://stackoverflow.com/a/38825553/2604529) –
@MarcelNovy 자체 서명 인증서를 사용하고 있습니다 ... 그게 ' 내 질문과 관련이 없다. – vesche
POST에서 현재 시도가 어떻게 작동하지 않는지 언급 한 경우 유용합니다. – superfell