Last active
December 26, 2015 04:49
-
-
Save lintianzhi/7096541 to your computer and use it in GitHub Desktop.
http.Post() 301 303
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
) | |
func handleGet(w http.ResponseWriter, req *http.Request) { | |
w.WriteHeader(200) | |
w.Write([]byte("Hello World")) | |
} | |
func handle303(w http.ResponseWriter, req *http.Request) { | |
http.Redirect(w, req, "/get", 303) | |
} | |
func handle302(w http.ResponseWriter, req *http.Request) { | |
http.Redirect(w, req, "/get", 302) | |
} | |
func handle301(w http.ResponseWriter, req *http.Request) { | |
http.Redirect(w, req, "/get", 301) | |
} | |
func main() { | |
http.HandleFunc("/301", handle301) | |
http.HandleFunc("/302", handle302) | |
http.HandleFunc("/303", handle303) | |
http.HandleFunc("/get", handleGet) | |
http.ListenAndServe(":7300", nil) | |
} | |
// client | |
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
func main() { | |
resp, _ := http.Post("http://127.0.0.1:7300/303", "", nil) | |
fmt.Println(resp) | |
resp, _ = http.Post("http://127.0.0.1:7300/302", "", nil) | |
fmt.Println(resp) | |
resp, _ = http.Post("http://127.0.0.1:7300/301", "", nil) | |
fmt.Println(resp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment