谁有好的网站推荐一个,seo外包如何,网页制作方法,济南企业营销型网站建设http#xff1a;无状态协议#xff0c;是互联网中使用http使用http实现计算机和计算机之间的请求和响应
使用纯文本方式发送和接受协议数据#xff0c;不需要借助专门工具进行分析就知道协议中的数据
服务器端的几个概念 Request#xff1a;用户请求的信息#xff0c;用…http无状态协议是互联网中使用http使用http实现计算机和计算机之间的请求和响应
使用纯文本方式发送和接受协议数据不需要借助专门工具进行分析就知道协议中的数据
服务器端的几个概念 Request用户请求的信息用来解析用户的请求信息包括 post、get、cookie、url 等信息 Response服务器需要反馈给客户端的信息 Conn用户的每次请求链接 Handler处理请求和生成返回信息的处理逻辑
http报文的组成
请求行请求头请求体响应头响应体
多种请求方式
GET:向服务器请求资源地址POST:直接返回请求内容HEAD:只要求响应头PUT:创建资源DELETE:删除资源TRACE:返回请求本身OPTIONS:返回服务器支持HTTP方法列表、CONNECT:建立网络连接PATCH :修改资源
软件模型
B/S结构,客户端浏览器/服务器,客户端是运行在浏览器中C/S结构,客户端/服务器,客户端是独立的软件
HTTP POST 简易模型 go对HTTP的支持
在golang的net/http包中提供了HTTP客户端和服务端的实现
Handle Func()可以设置函数的请求路径
LIstenAndServer实现监听服务
单控制器
发给处理器(Handler) 在Golang的net/http包下有ServeMutx实现了Front设计模式的Front窗口,ServeMux负责接收请求并把请求分发给处理器(Handler)
http.ServeMux实现了Handler接口
多控制器
在实际开发中大部分情况是不应该只有一个控制器的,不同的诗求应该交给不同的处理单元.
在Golang中支持两种多处理方式 多个处理器(Handler) 多个处理函数(HandleFunc)
使用多处理器
使用http.Handle把不同的URL绑定到不同的处理器在浏览器中输入http://localhost:8090/myhandler或http://ocalhost:8090/myother可以访问两个处理器 方法.但是其他URI会出现404(资源未找到)页面
package mainimport net/httptype MyHandler struct {
}
type MyHandle struct {
}func (m *MyHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte(MyHandle--第二个))
}func (m *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte(MyHandler))
}func main() {h : MyHandler{}h2 : MyHandle{}server : http.Server{Addr: localhost:8090}http.Handle(/first, h)http.Handle(/second, h2)server.ListenAndServe()
}使用多处理函数
func first(w http.ResponseWriter, r *http.Request) {fmt.Fprintln(w, 多函数first)
}
func second(w http.ResponseWriter, r *http.Request) {fmt.Fprintln(w, 多函数second)
}func main() {server : http.Server{Addr: localhost:8090}http.HandleFunc(/first, first)http.HandleFunc(/second, second)server.ListenAndServe()
}获取请求头
package mainimport (fmtnet/http
)func param(w http.ResponseWriter, r *http.Request) {h : r.Header //mapfmt.Fprintln(w, h)for _, n : range h[Accept-Language] {fmt.Fprintln(w, n)}
}func main() {server : http.Server{Addr: :8090}http.HandleFunc(/param, param)server.ListenAndServe()
}获取请求参数
可以一次性全部获取也可以按照名称获取