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,探寻更多的 ...
随机推荐
- ML.NET教程之客户细分(聚类问题)
理解问题 客户细分需要解决的问题是按照客户之间的相似特征区分不同客户群体.这个问题的先决条件中没有可供使用的客户分类列表,只有客户的人物画像. 数据集 已有的数据是公司的历史商业活动记录以及客户的购买 ...
- python 同时运行两个程序
linux的话: python py1.py & python py2/py &
- ASP.NET异步
1.ASP.NET线程模型 在WEB程序中,天生就是多线程的,我们知道,一个WEB服务可以同时服务器多个用户,我们可以想象一下,WEB程序应该运行于多线程环境中,对于运行WEB程序的线程,我们可以称之 ...
- [No0000134]C#中的委托,匿名方法和Lambda表达式
简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...
- Vue SSR配合Java的Javascript引擎j2v8实现服务端渲染1概述
原文地址 http://www.terwergreen.com/post/vue-ssr-j2v8-1.html 初步实现方案探索(Node环境) // 第 1 步:创建一个 Vue 实例 const ...
- 查找->静态查找表->折半查找(有序表)
文字描述 以有序表表示静态查找表时,可用折半查找算法查找指定元素. 折半查找过程是以处于区间中间位置记录的关键字和给定值比较,若相等,则查找成功,若不等,则缩小范围,直至新的区间中间位置记录的关键字等 ...
- LeetCode 590 N-ary Tree Postorder Traversal 解题报告
题目要求 Given an n-ary tree, return the postorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返 ...
- disruptor的并行用法
实现EventFactory,在newInstance方法中返回,ringBuffer缓冲区中的对象实例:代码如下: public class DTaskFactory implements Even ...
- elasticsearch-head的安装和使用
(一) 安装nodeJS, 下载nodejs安装包,直接下一步就可以,运行node -v 和npm -v有如下结果,表示安装成功. (二) 安装git(安装过程略过), 1.git clone git ...
- 元素class的增、删、查、toggle
比如有一个元素div <div class="btn user">我是div</div> 之前只知道元素有一个className可以来改动 元素的类名 但 ...