Go语言网络编程http请求
1. 概述
我们简单的描述一下什么是web服务的工作方式,通常一个客户端(客户端可以是浏览器或者其他任何能发送http请求得工具)请求一个URL(uniform resource locator ) ,如果这个url是域名那么首先会去请求DNS(Domain Name System) 获取域名对应的真实IP(InternetProtocol) ,通过IP找到对应的服务器,并与服务器建立TCP(Transmission Control Protocol) 连接,客户端向服务器发送 HTTP Request 数据包,服务服务器处理该请求后,向客户端响应HTTP response 包, 客户端接受这个响应,并做响应的处理,等全部接受之后,就断开这个TCP连接
HTPP协议就是超文本传输协议(HyperText Transfer Protocol ) HTTP协议承载于TCP协议之上的,也可以说HTTP协议的基础是TCP协议
HTTPS协议超文本传输安全协议(Hypertext Transfer Protocol Secure) 就是HTTP协议的安全版,在HTTP的协议基础上加入了TLS(Transport Layer Security 安全传输层协议)或者SSL(Secure Sockets Layer 安全套接层)协议层
通过Go语言来编写一个http服务器非常简单,同时通过Go语言发送http请求也很简单,我们使用 net/http 包就能实现
2. http服务端
package main
import (
"fmt"
"log"
"net/http"
)
func defaultFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println("client connect success ", r.RemoteAddr)
// 以json格式响应给客户端
fmt.Fprintf(w, "%v\n", "welcome to user")
}
func main() {
http.HandleFunc("/", defaultFunc)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServer: ", err)
}
}
3. https服务端
首先在本地构建https证书(windows10环境)
执行指令1
$ openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
..................+++++
........+++++
e is 65537 (0x010001)
执行指令2
$ openssl req -new -x509 -key server.key -out server.crt -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:demo1
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:PANG
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:
package main
import (
"fmt"
"net/http"
)
func MyFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.RemoteAddr)
fmt.Fprintf(w, "%s", "https request")
}
func main() {
http.HandleFunc("/", MyFunc)
cf := "E:/Go/src/GoNote/chapter9/demo12/main/server.crt"
ck := "E:/Go/src/GoNote/chapter9/demo12/main/server.key"
// 监听8081端口
http.ListenAndServeTLS(":8081", cf, ck, nil)
}
3. http客户端发送请求
发送http请求,请求方式有POST ,GET ,PUT, DELETE等
编写一个http web服务端用来处理客户端的请求
httpServer.go
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
type responseToClient struct {
Code int `json:"code"`
Message string `json:"message"`
Data map[string]string `json:"data"`
}
func defaultFunc(w http.ResponseWriter, r *http.Request) {
var dataForm map[string][]string
// 标识一个客户端的连接
fmt.Println("client connect success ", r.RemoteAddr)
// 获取地址栏内容
fmt.Println(r.Method, r.RequestURI)
// 获取请求头内容
for k, v := range r.Header {
fmt.Println(k, v[0])
}
data := make(map[string]string)
if err := r.ParseForm(); err == nil {
if r.Form != nil {
dataForm = r.Form
}
}
// 读取客户端的内容
buf := make([]byte, 2048)
n, _ := r.Body.Read(buf)
// 获取请求体中的内容
fmt.Println("receive data from body", string(buf[:n]))
if r.Method == "GET" {
// 解析参数
r.ParseForm()
for k, v := range r.Form {
data[k] = v[0]
}
}
// 处理客户端发送的POST请求和PUT请求
if r.Method == "POST" || r.Method == "PUT" {
ct, ok := r.Header["Content-Type"]
if ok {
// 如果是json数据根据请求头判断
if ct[0] == "application/json" {
json.Unmarshal(buf[:n], &data)
}
// 如果是POST表单数据
if ct[0] == "application/x-www-form-urlencoded" {
if dataForm != nil {
for k, v := range dataForm {
data[k] = v[0]
}
}
}
}
}
// 处理客户端的DELETE请求
// 记录当前时间 `2006-01-02 15:04:05` 是指的格式格式
data["time"] = time.Now().Format("2006-01-02 15:04:05")
m := responseToClient{200, "success", data}
mjson, e := json.Marshal(m)
if e != nil {
fmt.Println(e)
}
// 以json格式响应给客户端
fmt.Fprintf(w, "%v\n", string(mjson))
}
func main() {
http.HandleFunc("/", defaultFunc)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServer: ", err)
}
}
编写一能发送http请求的客户端包含 post,get,put,delete请求
httpClients.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
const BC = "http://127.0.0.1:8080/"
// 发送一个简单的http GET请求
func httpSimpleGet() {
resp, err := http.Get(BC + "index?aa=AA&bb=BB")
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
// 获取响应内容
resultByte, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(resultByte))
}
// 设置请求头和请求参数的Get请求
func httpGet() {
client := &http.Client{}
request, err := http.NewRequest("GET", BC, nil)
if err != nil {
log.Println(err)
}
// 在请求头中添加自定义数据
request.Header.Add("company", "PG")
request.Header.Add("appkey", "Test_0001")
// 添加请求参数
params := request.URL.Query()
params.Add("name", "pg")
params.Add("addr", "chain")
request.URL.RawQuery = params.Encode()
// 发送http请求,请求成功,获取响应
resp, err := client.Do(request)
if err != nil {
log.Println(err)
}
// 获取所有的响应内容
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
// 打印响应的内容
fmt.Println(string(result))
}
// http POST请求,并发送json数据
func httpPostJson() {
// post json 数据应用比较广泛
// 发送json数据,我们一般是用使用map或者结构体存储数据
// 然后转换成json数据
// 然后转换成byte数据,放在发送的body中一起发送
// 我们模拟一下这个过程
var std map[string]string = map[string]string{"work": "programmer", "skills": "golang", "addr": "北京"}
data, err := json.Marshal(std)
if err != nil {
log.Println(err)
}
body := bytes.NewBuffer([]byte(data))
req, err := http.NewRequest("POST", BC, body)
if err != nil {
log.Println(err)
}
// 设置请求头
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
defer resp.Body.Close()
if err != nil {
log.Println(err)
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
fmt.Println(string(result))
}
// 模拟post发送表单数据
func HttpPosForm() {
formData := url.Values{}
formData.Set("userName", "admin")
formData.Set("userPwd", "admin123456")
req, err := http.NewRequest("POST", BC, strings.NewReader(formData.Encode()))
if err != nil {
log.Println(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", strconv.Itoa(len(formData.Encode())))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(result))
}
// 模拟客户端发送PUT请求
func HttpPut() {
// 发送PUT请求和POST请求类似都可以发送json和form数据
std := map[string]string{"method": "PUT"}
data, err := json.Marshal(std)
if err != nil {
log.Println(err)
}
body := bytes.NewBuffer([]byte(data))
req, err := http.NewRequest("PUT", BC, body)
if err != nil {
log.Println(err)
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
defer resp.Body.Close()
if err != nil {
log.Println(err)
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
fmt.Println(string(result))
}
//模拟客户端发送DELETE请求
func HttpDelete() {
req, err := http.NewRequest("DELETE", BC, nil)
if err != nil {
log.Println(err)
}
// 添加请求参数 与发送get请求类似
params := req.URL.Query()
params.Add("user", "pahnaskdjalsdklasd")
req.URL.RawQuery = params.Encode()
// 发送http请求,请求成功,获取响应
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
}
// 获取所有的响应内容
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
// 打印响应的内容
fmt.Println(string(result))
}
func main() {
//httpSimpleGet()
//httpGet()
//httpPostJson()
//HttpPosForm()
//HttpPut()
HttpDelete()
}