首页 技术 正文
技术 2022年11月15日
0 收藏 428 点赞 2,763 浏览 2039 个字

实验1:上手篇

package mainimport (
"net/http"
//"fmt"
"os"
)func proxyFunc(response http.ResponseWriter, req *http.Request) {
os.Stdout.WriteString(req.Host + "\n")
response.Write([]byte(req.Host +"\n"))
return
}func main() {
http.HandleFunc("/", proxyFunc)
http.ListenAndServe(":9111", nil)
}

编译:go build proxy.go

执行:./proxy

客户端测试:curl curl http://127.0.0.1:9111/

测试输出:127.0.0.1:9111

实验2:获取request的body数据长度

package mainimport (
"net/http"
"fmt"
"io/ioutil"
)func proxyFunc(response http.ResponseWriter, req *http.Request) {
body, _ := ioutil.ReadAll(req.Body)
bodyLen := len(string(body))
response.Write([]byte(fmt.Sprintf("body len: %d\n", bodyLen)))
return
}func main() {
http.HandleFunc("/", proxyFunc)
http.ListenAndServe(":9111", nil)
}

执行:go build proxy.go && ./proxy

测试:curl –data “a=a” http://127.0.0.1:9111/

测试输出:body len: 3

实验3:做个中转机,前端请求,go中转给后端的服务

proxy.go代码

package mainimport (
"net/http"
//"fmt"
"io/ioutil"
//"strings"
)func proxyFunc(response http.ResponseWriter, req *http.Request) {
client := &http.Client{} path := req.URL.Path
query := req.URL.RawQuery url := "http://127.0.0.1:9112"
url += path
if len(query) > {
url += "?" + query
} proxyReq, err := http.NewRequest("POST", url, req.Body) if err != nil {
response.Write([]byte("http proxy request fail\n"))
return
} proxyReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
proxyReq.Header.Set("Cookie", "name=cookie") resp, err := client.Do(proxyReq)
defer resp.Body.Close() out, _ := ioutil.ReadAll(resp.Body)
response.Write(out)
}func main() {
http.HandleFunc("/", proxyFunc)
http.ListenAndServe(":9111", nil)
}

backend.go代码

package mainimport (
"io/ioutil"
"net/http"
)func procFunc(response http.ResponseWriter, req *http.Request) {
postBody, _ := ioutil.ReadAll(req.Body)
response.Write([]byte("query: " + req.URL.RawQuery + "\nbody: "))
response.Write(postBody)
response.Write([]byte("\n"))
response.Write([]byte("backend port: 9112\n"))
}func main() {
http.HandleFunc("/", procFunc)
http.ListenAndServe(":9112", nil)
}

proxy负责中转客户端请求,转到backend来处理,backend根据输入,直接打印其输出 query + post body

测试:curl –data “a=a”  http://127.0.0.1:9111/?b=b1111

测试输出:

query: b=b1111
body: a=a
backend port:

本篇文章测试了:go的http server,http server对应的post数据,和golang的http request能力,代码很简单

结束分隔符!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,037
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866