dongwu5743 2016-04-13 11:34
浏览 330

GoLang问题:如何在GoLang中传递key.pem,cert.pem,chain.pem证书

I am trying with the following code by passing cert.pem, key.pem and ..chain.pem but getting status:500 as the service is not able to read these certificates for https RESTful webservice. I went through all the examples in net and tried but still getting same exception.

package main

import (
    "crypto/tls"
    "crypto/x509"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "strings"
    "io"        
)

func main() {

    UserName := "user"
    appName := "AP"
    PasswordKey := "key"
    ServerName := "server"
    Sid := "sid" 

    // Load client cert
    cert, err := tls.LoadX509KeyPair("/cert.pem", "/key.pem")
    if err != nil {
        log.Fatal(err)
    }

    // Load CA cert
    caCert, err := ioutil.ReadFile("/xyz.chain.pem")
    if err != nil {
        log.Fatal(err)
    }
    caCertPool := x509.NewCertPool()
    ok := caCertPool.AppendCertsFromPEM(caCert)
    if !ok {
        panic("failed to parse root certificate")
    }

    fmt.Println("CERT: ",cert)

    // Setup HTTPS client
    tlsConfig := &tls.Config{
        Certificates: []tls.Certificate{cert},
        ServerName: "serverName",
        RootCAs:      caCertPool,
    }

    tlsConfig.BuildNameToCertificate()
    transport := &http.Transport{TLSClientConfig: tlsConfig}
    client := &http.Client{Transport: transport}
    fmt.Println("CLIENT: ",client)

 r := strings.NewReader("{\"userName\" : \"" +UserName+"\",\"appName\" : \""+appName+"\",\"passwordKey\" : \""+PasswordKey+"\",\"serverName\" : \""+ServerName+"\",\"sid\" : \""+Sid+"\"}")
        lr := io.Reader(r)


    resp, err := client.Post("https://xzytr/password/txt","application/json",lr)
    if err != nil {
        fmt.Println(err)
    }

    contents, err := ioutil.ReadAll(resp.Body)
    fmt.Printf("%s
", string(contents))
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用C语言输入方程怎么
    • ¥15 网站显示不安全连接问题
    • ¥15 github训练的模型参数无法下载
    • ¥15 51单片机显示器问题
    • ¥20 关于#qt#的问题:Qt代码的移植问题
    • ¥50 求图像处理的matlab方案
    • ¥50 winform中使用edge的Kiosk模式
    • ¥15 关于#python#的问题:功能监听网页
    • ¥15 怎么让wx群机器人发送音乐
    • ¥15 fesafe材料库问题