一、form截取登陆信息
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"strings"
)
//定义一个函数
func Hello(w http.ResponseWriter,r *http.Request) {
//解析url传递的参数,post请求则解析响应包的主体
r.ParseForm()
//获取表单数据
fmt.Println(r.Form)
//获取url的路径
fmt.Println("path",r.URL.Path)
fmt.Println("scheme",r.URL.Scheme)
fmt.Println(r.Form["url_long"])
v := url.Values{}
v.Set("name","Tom")
v.Add("friend","Jess")
v.Add("friend","Zoe")
fmt.Println(v.Get("name"))
fmt.Println(v.Get("friend"))
fmt.Println(v["friend"])
for key, value := range r.Form {
fmt.Println("key:",key)
fmt.Println("val:",strings.Join(value,""))
}
fmt.Fprintf(w,"Hello my route")
}
func login(w http.ResponseWriter,r *http.Request) {
//获取请求方法
fmt.Println("method:",r.Method)
r.ParseForm()
if r.Method == "GET" {
files, err := template.ParseFiles("D:/goproject/src/webDemo/http/form/login.html")
if err != nil {
panic(err)
}
files.Execute(w,nil)
}else {
//请求登录数据
fmt.Println("username:",r.Form["username"])
fmt.Println("password",r.Form["password"])
}
}
func main() {
//设置访问路由
http.HandleFunc("/hello",Hello)
//设置访问路由
http.HandleFunc("/login",login)
//设置监听的端口
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe:",err)
}
}
二、登陆页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录界面</title>
</head>
<body>
<form action="http://127.0.0.1:8080/login" method="post">
用户名:<input type="text" name="username"><br>
密   码:<input type="password" name="password"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
?
|