可以看优酷视频,qq视频好像有点问题
package main import ( "io" "io/ioutil" "log" "net/http" "os" "fmt" ) func handler(w http.ResponseWriter, r *http.Request) { var ( resp *http.Response data []byte err error ) r.RequestURI = "" r.ParseForm() resp, err = http.DefaultClient.Do(r) if err != nil { http.NotFound(w, r) log.Println("1、NotFound") return } data, err = ioutil.ReadAll(resp.Body) if err != nil && err != io.EOF { http.NotFound(w, r) log.Println("2、NotFound") return } for i, j := range resp.Header { for _, k := range j { w.Header().Add(i, k) log.Println("Header:",i,"=",k) } } for _,c := range resp.Cookies(){ w.Header().Add("Set-Cookie",c.Raw) log.Println("Set-Cookie",c.Raw) } _, ok := resp.Header["Content-Length"] if !ok && resp.ContentLength>0 { w.Header().Add("Content-Length",fmt.Sprint(resp.ContentLength)) log.Println("1、Content-Length",resp.ContentLength) }else{ log.Println("2、Content-Length",resp.Header["Content-Length"]) } log.Printf("resp.StatusCode:%d len:%d\n",resp.StatusCode,len(data)) w.WriteHeader(resp.StatusCode) w.Write(data) } func main() { http.HandleFunc("/", handler) log.Println("Start serving on port 8888") http.ListenAndServe(":8888", nil) os.Exit(0) }
有疑问加站长微信联系(非本文作者)