当请求http://localhost:8080/html/home.html 自动转发请求到 http://192.168.0.1/html/home.html,带cookie请求,不过cookie要每次都手工抓取
package main import ( "io/ioutil" "log" "net/http" // "strings" ) func statistic(w http.ResponseWriter, r *http.Request) { //r.URL.RequestURI() client := &http.Client{} req, err := http.NewRequest("GET", "http://192.168.0.1"+r.URL.Path, nil) if err != nil { log.Fatal(err) } req.Header.Set("Cookie", `SessionID=y151QXjszzoP5D2Lvfun0S/JPnFfquDtASLyWz+ZiSg6ngSt1gpakE2DChVN+hnfFOS6rFJ6gwUL+y0pAG+RpYYSogAFcZsuN919FXZle45UGP+ka6fZmceI9ew7Mj4Z;path=/;HttpOnly;`) resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } w.Header().Set("Content-Type", resp.Header.Get("Content-Type")) w.Write(body) } func main() { mux := http.NewServeMux() mux.HandleFunc("/", statistic) err := http.ListenAndServe(":8080", mux) if err != nil { log.Fatal("ListenAndServe:", err) } }