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

posted @ 2019-04-29 20:32  linengier  阅读(350)  评论(0编辑  收藏  举报