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,探寻更多的 ...
随机推荐
- cobbler搭建本地的yum仓库源
cobbler自动化安装参考文档 https://www.cnblogs.com/minseo/p/8537266.html 使用cobbler可以快速搭建一个本地的yum仓库 cobbler rep ...
- poj3613 Cow Relays【好题】【最短路】【快速幂】
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions:9207 Accepted: 3604 Descrip ...
- ASP.NET Core Web API处理HttpResponseMessage类型返回值的问题
在将我们的 web api 从 .NET Framework 迁移至 .net core(asp.net core 1.1)之后,遇到一个问题. 之前返回值类型为 HttpResponseMessag ...
- ubuntu系统中java -version所显示的版本与/etc/profile中配置的$JAVA_HOME的关系
问题产生 ubuntu 18.04环境下,执行 java -version 发现与/etc/profile中的$JAVA_HOME所设置的java版本不同. 推测原因 最近用apt install 安 ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- [No000013C]B树、B-树、B+树、B*树
B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...
- NodeJS websocket qr based on location
https://juejin.im/post/5a5728436fb9a01c982c7d93 http://www.cnblogs.com/panhe-xue/p/5902108.html---br ...
- Flink - Asynchronous I/O
https://docs.google.com/document/d/1Lr9UYXEz6s6R_3PWg3bZQLF3upGaNEkc0rQCFSzaYDI/edit // create the ...
- python作用域问题
今天出了个低级的错误,最后确定是作用域问题,特回顾知识点如下: 在Python程序中创建.改变.查找变量名时,都是在一个保存变量名的空间中进行,我们称之为命名空间,也被称之为作用域. Python的作 ...
- 使用脚本与orm模型交互对数据库操作
场景:如不想启动服务在框架中查看数据库数据,同时使用ORM框架对数据库操作带来的好处 import os import sys #将脚本所在的工程添加到环境变量 sys.path.append('.. ...