社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
废话少说:实现服务器打印输出客户端的请求参数,客户端打印服务器返回的数据
服务器:
package main
import (
"flag"
"fmt"
"net/http"
)
func main() {
host := flag.String("host", "127.0.0.1", "listen host")
port := flag.String("port", "8088", "listen port")
http.HandleFunc("/hello", Hello)
err := http.ListenAndServe(*host+":"+*port, nil)
if err != nil {
panic(err)
}
}
func Hello(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello World"))
fmt.Printf(" req.header=%+v,n req.usr.string=%+v,n req.url.path=%+vn", req.Header, req.URL.String(), req.URL.Path)
req.ParseForm()
d := req.Form
fmt.Println(d)
}
客户端:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
response, _ := http.Get("http://localhost:8088/hello?a=100")
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}
效果图:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!