http chunks 응답
func HandlePost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Connection", "Keep-Alive") w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("X-Content-Type-Options", "nosniff") ticker := time.NewTicker(time.Second) go func() { for t := range ticker.C { io.WriteString(w, "Chunk") fmt.Println("Tick at", t) } }() time.Sleep(time.Second * 5) ticker.Stop() fmt.Println("Finished: should return Content-Length: 0 here") w.Header().Set("Content-Length", "0") }
솔루션
func HandlePost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Connection", "Keep-Alive") w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("X-Content-Type-Options", "nosniff") ticker := time.NewTicker(time.Second) go func() { for t := range ticker.C { io.WriteString(w, "Chunk") fmt.Println("Tick at", t) } }() time.Sleep(time.Second * 5) ticker.Stop() fmt.Println("Finished: should return Content-Length: 0 here") w.Header().Set("Content-Length", "0") }문제를 해결하려면 :
$ telnet localhost 8080 ... HTTP/1.1 200 OK Date: ... Content-Type: text/plain; charset=utf-8 Transfer-Encoding: chunked 9 Chunk #1 9 Chunk #2 ...
전송 인코딩 헤더는 HTTP 작가가 자동으로 관리하므로 명시 적으로 설정하지 않습니다.
"FMT" "io" "통나무" "net/http" "시간" )) func main () { http.handlefunc ( "/", func (w http.responsewriter, r *http.request) { Flusher, OK : = w. (http.flusher) IF! OK { panic ( "예상 http.responsewriter가 http.flusher가 될 것으로 예상됩니다") } w.header (). set ( "x-content-type-options", "nosniff") i : = 1; I
$ telnet localhost 8080 ... HTTP/1.1 200 OK 날짜: ... 내용 유형 : 텍스트/일반; charset = utf-8 전송 인코딩 : 청크 9 청크 #1 9 청크 #2 ...
각 청크는 서버가 보내면서 점진적으로 수신됩니다.부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3