对于流式http端点有没有办法避免发送长度?
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Del("Content-Length")
Run Code Online (Sandbox Code Playgroud)
这是我回来的.
HTTP/1.1 200 OK
Content-Length: 0
Content-Type: image/jpeg
Date: Mon, 23 Jun 2014 10:00:59 GMT
Transfer-Encoding: chunked
Transfer-Encoding: chunked
Run Code Online (Sandbox Code Playgroud)
服务器也会打印警告.
2014/06/23 06:04:03 http: WriteHeader called with both Transfer-Encoding of "chunked" and a Content-Length of 0
Run Code Online (Sandbox Code Playgroud)
你不应该手动设置Transfer-Encoding.Go会为你做这件事,以及Content-Length.
curl,Go http客户端或任何标准http客户端将自动正确读取chunked或non-chunked http响应.
分块服务器的小例子:http://play.golang.org/p/miEV7URi8P
package main
import (
"io"
"log"
"net/http"
)
// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
for i := 0; i < 5; i++ {
io.WriteString(w, "hello, world!\n")
w.(http.Flusher).Flush()
}
}
func main() {
http.HandleFunc("/", HelloServer)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Run Code Online (Sandbox Code Playgroud)
在图像/ jpeg的情况下,您可以将分块决策委托给Go,或者从图像中手动发送N个字节,然后刷新.
| 归档时间: |
|
| 查看次数: |
1624 次 |
| 最近记录: |