go https json
好吧,再来一个看起来高档点的吧
自从知道 Go有本地调用后,我就回到windows了
哈哈,以下内容,均在win10下搞定
预备:先做两个文件,服务器端的私钥KEY和公钥证书
1. openssl genrsa -out server.key 2048
2. openssl req -new -x509 -key server.key -out server.crt -days 3650
3.现在你应该有了server.key和server.crt两个文件,因为是自签名证书,所以,把server.crt安装到浏览器受信任证书存储区(这个我不多说了,一堆知识,大家自己去百度吧)
一 GO https server
//https.go
package main
import (
"fmt"
"net/http"
"encoding/json"
)
type MyData struct {
Name string `json:"item"`
Other float32 `json:"amount"`
}
func handler(w http.ResponseWriter, r *http.Request) {
var detail MyData
detail.Name = "1"
detail.Other = 2
body, err := json.Marshal(detail)
if err != nil {
panic(err.Error())
}
fmt.Fprintf(w, string(body))
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("http server listens at 8086")
http.ListenAndServeTLS(":8086", "server.crt", "server.key", nil)
}
//启动服务
go run https.go
二 Go https client
//https -client.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/bitly/go-simplejson"
"crypto/tls"
"encoding/json"
)
func main() {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://localhost:8086")
//resp, err := http.Get("https://localhost:8086")
if err != nil {
fmt.Println("error:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
//decode json
js, err := simplejson.NewJson(body)
if err != nil {
panic(err.Error())
}
fmt.Printf("%T:%v\n",js,js)
type MyData struct {
Name string `json:"item"`
Other float32 `json:"amount"`
}
var res MyData
err = json.Unmarshal([]byte(body), &res)
fmt.Println(res)
fmt.Println(res.Name, res.Other)
}
//运行客户端
go run https-client.go
//结果
{"item":"1","amount":2}
*simplejson.Json:&{map[item:1 amount:2]}
{1 2}
1 2
三 browser client
//Microsoft Edge

Finally:
嗯,很好,很强大,可以考虑搞点儿RESTful 服务啦
你呢?
说你呢!
你想干吗?
多说一句:Go 做https客户端,响应速度明显慢于Edge。不知道为啥。还好,一般都用浏览器来干活嘛!无关紧要的啦。哈哈
go https json的更多相关文章
- VSCode package.json warning: Problems loading reference 'https://json.schemastore.org/package'...
报错内容 Problems loading reference 'https://json.schemastore.org/package': Unable to load schema from ' ...
- 后台web请求代码(含https,json提交)
后台web请求 namespace XXXX.Utilites { using System; using System.Collections.Generic; using System.IO; u ...
- JSON资料汇总
网络入门学习资料 1.W3School的JSON教程:http://www.w3school.com.cn/json/index.asp 2.Introducing JSON[介绍JSON]:http ...
- json.net json转换神器
json.nethttps://json.codeplex.com/ api documenthttp://james.newtonking.com/json/help/index.html#
- ballerina 学习二十 http/https
提供http && https server && client 访问功能 client endpoint 说白了就是http client 参考代码 import b ...
- Linq to JSON/Json.NET
Can I LINQ a JSON? http://stackoverflow.com/questions/18758361/can-i-linq-a-json Json.NET https://js ...
- 什么是json? 什么是xml?JSON与XML的区别比较
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它使得人们很容易的进行阅读和编写.同时也方便了机器进行解析和生成.它是基于 JavaScript Prog ...
- JavaScript 获得客户端IP
Below are all the free active IP lookup services I could find and the information they return. If yo ...
- es的返回数据结构
ES即简单又复杂,你可以快速的实现全文检索,又需要了解复杂的REST API.本篇就通过一些简单的搜索命令,帮助你理解ES的相关应用.虽然不能让你理解ES的原理设计,但是可以帮助你理解ES,探寻更多的 ...
随机推荐
- db2 Reorgchk:重组检查,是否需要重组
Reorgchk:重组检查,是否需要重组.判断表或索引是否需要重组,有2种方法:1.通过reorgchk工具 reorgchk工具利用8个公式(3个表公式,5个索引公式),如果表统计结果F1,F2或 ...
- 动态环境下的slam问题如何解决?
作者:颜沁睿链接:https://www.zhihu.com/question/47817909/answer/107775045来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- python 给字符串加颜色
msg = '\033[41;1m字符串内容\033[0m' print(msg) # \033[41;1m起始位置 改变41数值就是改变其他颜色,.033[0m 结束位置
- [No0000158]思维模型1-20
[No0000158]思维模型1-20.7z 思维模型No1|第一性原理 第一原理(又叫第一性原理)是个今年很火的概念,最早由亚里士多德提出,它相当于数学中的公理,即在每一个系统的探索中,存在第一原理 ...
- Angular4 —— NgModule
http://www.cnblogs.com/dojo-lzz/p/5878073.html
- 变长编码表 ASCII代码等长编码
小结: 1.ASCII编码.GBK编码不是变长编码: 2.数据压缩: 示例: aabacdab → 00100110111010 → |0|0|10|0|110|111|0|10| → aabacda ...
- Verilog HDL语言实现的单周期CPU设计(全部代码及其注释)
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- [dpdk] dpdk启动几个线程
看别人的代码搞得有点晕,突然有点不确定,再确认一次. 使用 helloworld程序测试一下. /root/dpdk-16.07/examples/helloworld 一: 只启动一个核心. [r ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- ipv6的校验格式
ipv6的校验格式: ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$