当前位置: 首页 > news >正文

恩施网站建设模板wordpress胖鼠采集

恩施网站建设模板,wordpress胖鼠采集,360浏览器建设银行网站,新桥企业网站建设概述 因为工作需要协助修改某个golang程序,添加双向认证。但是在调整的过程遇到一个HTTP POST请求变成GET诡异的问题,最后各种搜索,总算解决,博文记录,用于备忘。 代码 服务端 因工作内容,代码有删减&a…

概述

因为工作需要协助修改某个golang程序,添加双向认证。但是在调整的过程遇到一个HTTP POST请求变成GET诡异的问题,最后各种搜索,总算解决,博文记录,用于备忘。

代码

服务端

因工作内容,代码有删减,以下样例

package mainimport ("crypto/tls""crypto/x509""encoding/json""fmt""io""io/ioutil""log""math/rand""mime""mime/multipart""net/http""os""strings""time"
)func main() {mux := http.NewServeMux()mux.HandleFunc("/...", handleToken)mux.HandleFunc("/...", handleHandshake)mux.HandleFunc("/...", handleData)caCert, err := os.ReadFile("certs/root_cert.pem")if err != nil {log.Fatalf("Reading server certificate: %s", err)}caCertPool := x509.NewCertPool()if !caCertPool.AppendCertsFromPEM(caCert) {fmt.Print("AppendCertsFromPEM failured!!!")}// Create TLS configuration with the certificate of the servertlsConfig := &tls.Config{ClientCAs:  caCertPool, //载入验证客户端证书的根证书ClientAuth: tls.RequireAndVerifyClientCert, //设置需要客户端证书}h1s := &http.Server{Addr:      ":8008",Handler:   mux,TLSConfig: tlsConfig,}log.Fatal(h1s.ListenAndServeTLS("certs/server_cert.pem", "certs/server_key.pem"))
}...
...
...func handleToken(w http.ResponseWriter, r *http.Request) {log.Printf("Token......%s", r.Method)switch r.Method {case http.MethodPost:...case http.MethodDelete:...default:http.Error(w, "400 Unsupport Method", http.StatusBadRequest)}}func handleHandshake(w http.ResponseWriter, r *http.Request) {.....
}

客户端

func GetToken(client http.Client) error {authData := ...jsonStr, _ := json.Marshal(authData)resp, err := client.Post(srv+url, "application/json", bytes.NewBuffer(jsonStr))if err != nil {log.Printf("Failed get token: err:%s \n", err)return err}defer resp.Body.Close()....return nil
}func main() {... client := http.Client{}// Create a pool with the server certificate since it is not signed// by a known CAcaCert, err := os.ReadFile("certs/root_cert.pem")if err != nil {log.Fatalf("Reading server certificate: %s", err)}caCertPool := x509.NewCertPool()caCertPool.AppendCertsFromPEM(caCert)clientCert, err := tls.LoadX509KeyPair("certs/client_cert.pem", "certs/client_key.pem")if err != nil {panic(err)}// Create TLS configuration with the certificate of the servertlsConfig := &tls.Config{RootCAs:            caCertPool,Certificates:       []tls.Certificate{clientCert},InsecureSkipVerify: true, //真实证书的情况下,需要删除该行,自签名可以保留}client.Transport = &http2.Transport{TLSClientConfig: tlsConfig}GetToken(client)
}

Q&A

Q:GetToken发起请求Post请求在服务端收到变成了GET
A:后面经过排查,根源是URL拼接的时候,中间多了一个“/”字符串,原本URL https://127.0.0.1:8008/api/token错误拼接成https://127.0.0.1:8008//api/token,从而在服务器端触发了301响应,go的http client在处理301响应的时候,将POST方法改成GET重新提交,所以导致服务端收到的请求方法是GET而不是POST

对应代码位于client.go

// redirectBehavior describes what should happen when the
// client encounters a 3xx status code from the server.
func redirectBehavior(reqMethod string, resp *Response, ireq *Request) (redirectMethod string, shouldRedirect, includeBody bool) {switch resp.StatusCode {case 301, 302, 303:redirectMethod = reqMethodshouldRedirect = trueincludeBody = false// RFC 2616 allowed automatic redirection only with GET and// HEAD requests. RFC 7231 lifts this restriction, but we still// restrict other methods to GET to maintain compatibility.// See Issue 18570.if reqMethod != "GET" && reqMethod != "HEAD" {redirectMethod = "GET"}

Q:go run 执行程序,报以下错误

/usr/local/go/pkg/tool/linux_arm64/link: running gcc failed: exit status 1
/usr/bin/ld: 找不到 -l***
collect2: 错误:ld 返回 1

A:因为程序内部通过cgo调用了第三方的动态库,而该动态库又没有在ldconfig或者系统默认目录下,所以找不到对应库导致编译出错,可以通过以下命令临时指定并执行

CGO_LDFLAGS="-L第三方动态库所在路径 -O2 -g" go run test.go

参考链接

Http Post请求被强制转换为Http Get请求
自签名根证书、中间证书、服务器证书生成流程详解

http://www.yayakq.cn/news/39897/

相关文章:

  • wordpress多站点换域名中山市做网站
  • 网站模版 优帮云做设计英文网站
  • 有什么做任务的网站吗坦克大战网站开发课程设计报告
  • wordpress要装在根目录深圳搜索seo优化排名
  • 怎么在百度建设一个网站顺德购物网站建设
  • 公司网站设计规范企业网络建设公司排名
  • wordpress企业网站seo虚拟主机销售网站模板
  • 太原市住房和城乡建设局的网站专业建设企业网站
  • 京东网站内容建设k网站建设
  • 杭州自助建站模板下载ssc网站建设教程
  • 深圳市手机网站建设报价做网站策划需要用什么软件
  • 曲阜住房城乡建设局网站个人养老保险查询
  • 建网站策划方案广西建设银行行号查询网站
  • 安徽二建注销网站在哪查询上海有限公司有哪些
  • 做的网站如何防止怕爬虫测网站打开的速度的网址
  • 马云1688网站在濮阳如何做四川建设人才考试网官方网站
  • 吉林市 网站建设网站服务器设置地点
  • 专用网站建设河北建设厅八大员报名网站
  • 制作网站软件深圳大型网络科技公司
  • 工程承包商赚钱吗广州seo排名外包
  • 网站虚拟主持人辽宁省建设工程信息网官网新系统
  • 响应式mvc企业网站源码住房和城乡建设部网站八大员
  • 浙江工信部网站备案查询工程师招聘网站
  • 昆明企业免费建站网站自主制作平台
  • 广东城乡住房建设部网站十大经典营销案例
  • 综合商城网站程序运城网站建设公司有多少钱
  • 美妆网站开发背景宁波网站建设设计公司信息
  • 北京网页设计公司兴田德润团队长春做网站seo的
  • 阿里云服务器上做网站网站备案收费么
  • 西安php网站建设建设银行梅州分行网站