-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_post.go
89 lines (79 loc) · 2.24 KB
/
http_post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package net
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/e-chain-net/echain-server-go-demo/common"
"io/ioutil"
"net/http"
"strconv"
"time"
)
func HttpPost(url string,payload []byte) ([]byte,error){
timeStamp := strconv.FormatInt(common.GetTimeMilli(),10);
rsaPrivate,err := common.LoadRsaPrivateKey(common.RsaPrivate)
if err != nil{
return nil,err
}
signature,err := common.RsaSign(rsaPrivate,[]byte(common.MerchantNo + "-" + timeStamp))
if err != nil{
return nil,err
}
// Create request payload
//payload := []byte(`{"jsonRpc": {"method":"getBlockLimit","params":[]}}`)
//fmt.Println("url:" + url)
// Create a new HTTP request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
//fmt.Println("Error creating HTTP request:", err)
return nil,err
}
// Set content type header
req.Header.Set("Accept", "*/*")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
req.Header.Set("Connection", "Keep-Alive")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("charset", "utf-8")
req.Header.Set("merchantNo",common.MerchantNo)
req.Header.Set("sign",signature)
req.Header.Set("timestamp",timeStamp)
// Load server certificate
certPool := x509.NewCertPool()
pemData, err := ioutil.ReadFile(common.CertPath)
if err != nil {
return nil,err
}
ok := certPool.AppendCertsFromPEM(pemData)
if !ok {
return nil,err
}
// Create a TLS configuration
tlsConfig := &tls.Config{
RootCAs: certPool,
}
// 创建不验证SSL证书的Transport,后面上了主网还是要开启验证的
transport := &http.Transport{
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSClientConfig: tlsConfig,
}
// Send HTTP request
client := &http.Client{
Timeout: time.Second * 5,
Transport: transport,
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending HTTP request:", err)
return nil,err
}
defer resp.Body.Close()
//Print HTTP response status code and body
//fmt.Println("Response Status:", resp.Status)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading HTTP response body:", err)
return nil,err
}
return body,nil
}