Web的使用.接收请求&处理请求

通过处理器处理请求
处理单个请求
http://localhost:8080/anything/at/all,同样会看到相同的Hello World响应。
package main
import (
"fmt"
"net/http"
)
type MyHandler struct{}
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
}
func main() {
handler := MyHandler{}
server := http.Server{
Addr:"127.0.0.1:8080",
Handler:&handler,
}
server.ListenAndServe()
}
使用多个处理器
现在,访问地址http://localhost:8080/hello将会看到“Hello!”,而访问地http://localhost:8080/world则会看到“World!”。
package main
import (
"fmt"
"net/http"
)
type HelloHandler struct{}
func (h *HelloHandler) ServeHTTP (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello!")
}
type WorldHandler struct{}
func (h *WorldHandler) ServeHTTP (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "World!")
}
func main() {
hello := HelloHandler{}
world := WorldHandler{}
server := http.Server{
Addr:"127.0.0.1:8080",
}
http.Handle("/hello", &hello)
http.Handle("/world", &world)
server.ListenAndServe()
}
使用处理函数
package main
import (
"fmt"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello!")
}
func world(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "World!")
}
func main() {
server := http.Server{
Addr:"127.0.0.1:8080",
}
http.HandleFunc("/hello", hello)
http.HandleFunc("/world", world)
server.ListenAndServe()
}

为了提取表单传递的键值对数据,用户可能需要亲自对服务器接收到的未经处理的表单数据进行语法分析。但事实上,因为net/http库已经提供了一套用途相当广泛的函数,这些函数一般都能够满足用户对数据提取方面的需求

使用Request结构的方法获取表单数据的一般步骤是:

(1)调用ParseForm方法或者ParseMultipartForm方法,对请求进行语法分析。
(2)根据步骤1调用的方法,访问相应的Form字段、PostForm字段或MultipartForm字段。

package main
import (
"fmt"
"net/http"
)
func process(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Fprintln(w, r.Form)
}
func main() {
server := http.Server{
Addr:"127.0.0.1:8080",
}
http.HandleFunc("/process", process)
server.ListenAndServe()
}
这段代码中最重要的就是下面这两行:
r.ParseForm()
fmt.Fprintln(w, r.Form)
如前所述,这段代码首先使用了ParseForm方法对请求进行语法分析,然后再访问Form字段,获取具体的表单。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GoWebProgramming</title>
</head>
<body>
<form action=http://127.0.0.1:8080/process?hello=world&thread=123 
➥method="post" enctype="application/x-www-form-urlencoded">
<input type="text" name="hello" value="sau sheong"/>
<input type="text" name="post" value="456"/>
<input type="submit"/>
</form>
</body>
</html>

这个HTML表单可以完成以下工作:
·通过POST方法将表单发送至地址http://localhost:8080/process?hello=world&thread=123;
·通过enctype属性将表单的内容类型设置为application/x-www-form-urlencoded;
·将hello=sau sheong和post=456这两个HTML表单键值对发送至服务器。

获取值的方法有

可以获取表的和URL的键值对
r.ParseForm()
fmt.Fprintln(w, r.Form)
只获取表单键值对而不是URL键值对,
r.ParseForm()
fmt.Fprintln(w, r.PostForm)
为了取得multipart/form-data编码的表单数据
MultipartForm字段只包含表单键值对而不包含URL键值对,
r.ParseMultipartForm(1024)
fmt.Fprintln(w, r.MultipartForm)

MultipartForm字段的值也不再是一个映射,而是一个包含了两个映射的结构,其中第一个映射的键为字符串,值为字符串组成的切片,而第二个映射则是空的——这个映射之所以会为空,是因为它是用来记录用户上传的文件的,
FormValue方法允许直接访问与给定键相关联的值
fmt.Fprintln(w,r.FormValue("hello"))
PostFormValue方法只会返回表单键值对而不会返回URL键值对。
fmt.Fprintln(w, r.PostFormValue("hello"))
对文件进行解析:
使用FormFile方法获取被上传的文件
file, _, err := r.FormFile("uploaded")
if err == nil {
data, err := ioutil.ReadAll(file)
if err == nil {
fmt.Fprintln(w, string(data))
}
t, _ := template.ParseFiles("tmpl.html")
t.Execute(w, "Hello World!")
ResponseWriter:

ResponseWriter接口拥有以下3个方法:
·Write;
·WriteHeader;
·Header。

使用Write方法向客户端发送响应
func writeExample(w http.ResponseWriter, r *http.Request) {
str := `<html>
<head><title>Go Web Programming</title></head>
<body><h1>Hello World</h1></body>
</html>`
w.Write([]byte(str))
}
通过WriteHeader方法将状态码写入到响应当中
w.WriteHeader(501)
fmt.Fprintln(w, "No such service, try next door")
通过编写首部实现客户端重定向
func headerExample(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "http://google.com")
w.WriteHeader(302)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值