好吧,再来一个看起来高档点的吧

自从知道 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的更多相关文章

  1. 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 ' ...

  2. 后台web请求代码(含https,json提交)

    后台web请求 namespace XXXX.Utilites { using System; using System.Collections.Generic; using System.IO; u ...

  3. JSON资料汇总

    网络入门学习资料 1.W3School的JSON教程:http://www.w3school.com.cn/json/index.asp 2.Introducing JSON[介绍JSON]:http ...

  4. json.net json转换神器

    json.nethttps://json.codeplex.com/ api documenthttp://james.newtonking.com/json/help/index.html#

  5. ballerina 学习二十 http/https

    提供http && https server && client 访问功能 client endpoint 说白了就是http client 参考代码 import b ...

  6. Linq to JSON/Json.NET

    Can I LINQ a JSON? http://stackoverflow.com/questions/18758361/can-i-linq-a-json Json.NET https://js ...

  7. 什么是json? 什么是xml?JSON与XML的区别比较

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它使得人们很容易的进行阅读和编写.同时也方便了机器进行解析和生成.它是基于 JavaScript Prog ...

  8. JavaScript 获得客户端IP

    Below are all the free active IP lookup services I could find and the information they return. If yo ...

  9. es的返回数据结构

    ES即简单又复杂,你可以快速的实现全文检索,又需要了解复杂的REST API.本篇就通过一些简单的搜索命令,帮助你理解ES的相关应用.虽然不能让你理解ES的原理设计,但是可以帮助你理解ES,探寻更多的 ...

随机推荐

  1. 思科交换机根据mac地址限制主机

    思科交换机可以根据客户端mac地址限制访问 查看对应IP的mac地址及vlan show arp | in 10.1.62.188 进入设置模式 configure terminal mac addr ...

  2. ESP8266 的几个代码 加深对LUA的理解

    --some functions dofile("functions.lua") lighton1= lighton2= lighton3= lighton4= pin1 = pi ...

  3. VS2015工具箱不出现ArcGIS Windows Forms怎么办?

    原文: https://blog.csdn.net/pangpi814961437/article/details/7954033

  4. Glide加载图片报错You must not call setTag() on a view Glide is targeting

    报错信息为:You must not call setTag() on a view Glide is targeting 原因就是View使用setTag后导致Glide之前请求的标记被清除,强制转 ...

  5. 如何解决selenium打开chrome提示chromedriver.exe已停止工作

    场景:启动Chrome,打开URL,提示“disconnected: unable to connect to renderer” 解决方法:chromedriver与chrome的对应关系表, 需要 ...

  6. Nor Flash 驱动框架

    框架入口源文件: lcd.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6      硬件平台:JZ2440 以下是驱动框架: 以下是驱动代码 s3c_nor_ ...

  7. FastDFS的使用

    1.FastDFS 1.1. 什么是FastDFS? FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用 ...

  8. svn 客户端安装 windows

    windows使用的 https://tortoisesvn.net/ 下载中文语言包 安装 安装完安装语言包 看到这个代表svn客户端可以用了 windows客户端下载TortoiseSVN软件进行 ...

  9. 要继续看Python写算法的内容请到那里去

    由于在这里发文章的时候.莫名其妙的出现公布出去的问题.客服告知是由于链接或者敏感词. 能不能告诉我哪里出了问题?我能够改动,以便再发. 可是,没有人告诉我.仅仅是告诉我不能发. 另外,能不能公布一下敏 ...

  10. LVS集群简介及使用

    什么是集群 一组通过高速网络互联的计算组,并以单一系统的模式加以管理 将很多服务器集中在一起,提供一种服务,在客户端看来就象是只有一个服务器 可以在付出较低成本的情况下获得在性能,可靠性,灵活性方面的 ...