• golang (5) http 请求分析


    http 分析包分析

    	fmt.Println("get Content-Type: ", r.Header.Get("Content-Type"))
    	var reader io.Reader = r.Body
    	b, e := ioutil.ReadAll(reader)
    	if e != nil {
    		fmt.Println("get body: ", string(b))
    	}
    	fmt.Println("get body: ", string(b))
    	err := r.ParseForm()
    	if err != nil {
    		common.HttpRspSend(w, STATUS_BAD_PARAM, "param parse is error: "+err.Error())
    		return
    	}
    
    	if len(r.Form) == 0 {
    		fmt.Println("no from ")
    		common.HttpRspSend(w, STATUS_BAD_PARAM, "no request param ")
    		return
    	}
    	if len(r.PostForm) == 0 {
    		fmt.Println("no post param")
    		common.HttpRspSend(w, STATUS_BAD_PARAM, "no request param ")
    		return
    	}
    
    	form := r.PostForm
    	fmt.Println("get cmd is ", form.Get("cmd"))
    
    	if r.Method != "POST" {
    		common.HttpRspSend(w, STATUS_BAD_PARAM, "Only POST allowed!")
    		return
    	}
    	log.Info(pretty.Sprint(form))
    
    

    https  客户端跳过http认证

    golang http请求server的https文件,出现错误
    error: certificate signed by unknown authority,
    go的Client端默认要对服务端传过来的数字证书进行校验的,如果这个证书是由不知名CA签发的,则会出先上面的错误。联想到curl的-k选项,可以跳过对服务端认证的认证。
    修改client.go的代码,让client端略过对证书的校验,如下:

    package main
    
    import (
        "crypto/tls"
        "fmt"
        "io/ioutil"
        "net/http"
    )
    
    func main() {
        tr := &http.Transport{
            TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
        }
        client := &http.Client{Transport: tr}
        resp, err := client.Get("https://localhost:8081")
    
        if err != nil {
            fmt.Println("error:", err)
            return
        }
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        fmt.Println(string(body))
    }
    

    http urlencode

    欢迎评论交流
  • 相关阅读:
    下载并安装chrome插件的方法
    QString转化为char *的方式
    a socket demo
    TCP/IP相关知识总结(马士兵教育)视频对应图片
    C++中的按位或的用意
    dll路径加载顺序
    【第二周】【作业七】四人小组项目
    【第二周】【作业六】结对项目,四则运算++
    【第二周】【作业八】个人项目词频统计++
    【第二周】【作业三】效能测试
  • 原文地址:https://www.cnblogs.com/linengier/p/10792719.html
Copyright © 2020-2023  润新知