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

自从知道 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. spring的自生一个bug

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. db2 Reorgchk:重组检查,是否需要重组

    Reorgchk:重组检查,是否需要重组.判断表或索引是否需要重组,有2种方法:1.通过reorgchk工具  reorgchk工具利用8个公式(3个表公式,5个索引公式),如果表统计结果F1,F2或 ...

  3. 微信小程序学习——框架视图层(view)

    视图层是有WXML与WXSS编写的,由组件来进行展示. WXML(WeiXin Markup Language)用于写页面结构的. WXSS(WeiXin Style Sheet)用于页面的样式. 组 ...

  4. Cnblog Markdown编辑器

    第一次使用Cnblog Markdown编辑器 1.在博客园使用 Markdown 与 LATEX[1] 1.1.开启MarkDown编辑器 选择 Markdown 撰写博文:在博客园管理-选项中,将 ...

  5. Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。

    题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...

  6. en-zh(科学技术)science and technology

    S Korea to roll out 5G韩国正式推5G商用服务 South Korea will become the first country to commercially launch f ...

  7. mysql查询表死锁和结束死锁的方法

    1.查询是否锁表 show open tables where in_use>0; 2.查询进程 show processlist 查询到相对应的进程,然后 kill id 3.查看正在锁的事务 ...

  8. Python中给List添加元素的4种方法

    https://blog.csdn.net/hanshanyeyu/article/details/78839266 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持 ...

  9. JAVA常用的异常处理情况

    从编程到现在,遇见过很多次程序崩的情况,好多时候都不知道怎么去解决才好,一般性解决就是百度或者问别人,但是每一次百度解决的下一次还是会遇见同样的问题,也没有系统的整理梳理过相关的处理异常的知识,再一次 ...

  10. scapy 中的ARP

    scapy 常用命令 ls() //查看支持的协议对象lsc() //查看函数show() //查看数据包结构send() //三层发包sr() //三层收发sr1() //三层收发只收一 timeo ...