参考链接:

https://www.jb51.net/article/115693.htm

https://www.jb51.net/article/60900.htm

https://www.cnblogs.com/5bug/p/8494953.html

1、服务器解析GET请求,返回值为文本格式

package main

import (
"log"
"net/http"
) func checkToken(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.Form["token"][0] == "chending123" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("验证成功!"))
} else {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("验证失败!"))
}
} func main() {
http.HandleFunc("/user/check", checkToken)
er := http.ListenAndServe("localhost:9090", nil)
if er != nil {
log.Fatal("ListenAndServe: ", er)
}
}

2、返回值为json格式

package main

import (
"log"
"net/http"
"encoding/json"
) func checkToken(w http.ResponseWriter, r *http.Request) {
r.ParseForm() var result ResponseJson if r.Form["token"][] == "chending123" {
w.WriteHeader(http.StatusOK)
result.Data = "chending"
result.Message = "验证成功!"
} else {
w.WriteHeader(http.StatusNotFound)
result.Message = "验证失败!"
} bytes, _ := json.Marshal(result)
w.Write(bytes)
} func main() {
http.HandleFunc("/user/check", checkToken)
er := http.ListenAndServe("localhost:9090", nil)
if er != nil {
log.Fatal("ListenAndServe: ", er)
}
} type ResponseJson struct {
Data string
Message string
}

3、解析POST请求与解析GET请求方法一致,POST格式为x-www-form-urlencoded

默认地,表单数据会编码为 "application/x-www-form-urlencoded"

4、GET请求样式

http://localhost:9090/user/check?token=chending123

http://localhost:9090/user/confirm?user=chending&pass=123456

5、POST请求样式

http://localhost:9090/user/confirm

以application/json格式发送数据

{

  “user”: "chending",

  "pass": "123456"

}

6、Go的GET发送

代码如下:
package main
import (
"fmt"
"net/url"
"net/http"
"io/ioutil"
"log"
)
func main() {
u, _ := url.Parse("http://localhost:9001/xiaoyue")
q := u.Query()
q.Set("username", "user")
q.Set("password", "passwd")
u.RawQuery = q.Encode()
res, err := http.Get(u.String());
if err != nil {
log.Fatal(err) return
}
result, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err) return
}
fmt.Printf("%s", result)
}

7、Go的POST发送

package main
import (
"fmt"
"net/url"
"net/http"
"io/ioutil"
"log"
"bytes"
"encoding/json"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
ServersID string
}
func main() {
var s Serverslice
var newServer Server;
newServer.ServerName = "Guangzhou_VPN";
newServer.ServerIP = "127.0.0.1"
s.Servers = append(s.Servers, newServer)
s.Servers = append(s.Servers, Server{ServerName: "Shanghai_VPN", ServerIP: "127.0.0.2"})
s.Servers = append(s.Servers, Server{ServerName: "Beijing_VPN", ServerIP: "127.0.0.3"})
s.ServersID = "team1"
b, err := json.Marshal(s)
if err != nil {
fmt.Println("json err:", err)
}
body := bytes.NewBuffer([]byte(b))
res,err := http.Post("http://localhost:9001/xiaoyue", "application/json;charset=utf-8", body)
if err != nil {
log.Fatal(err)
return
}
result, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("%s", result)
}

8、GET设置请求头

    client := &http.Client{}
url := "http://localhost:9090/tokenconfirm" reqest, err := http.NewRequest("GET", url, nil) reqest.Header.Set("Content-Type", "application/json")
reqest.Header.Add("AccessToken", token) if err != nil {
panic(err)
} res, err := client.Do(reqest) defer res.Body.Close() jsonStr, err := ioutil.ReadAll(res.Body) if err != nil {
log.Fatal(err)
}

最新文章

  1. ThinkPHP的URL访问
  2. ferret不能创建txt文本
  3. c++实现简单的链表
  4. Web 前端开发学习之路(入门篇)
  5. MySQL数据库(表)的导入导出(备份和还原)
  6. Windows Azure 使用体验
  7. PHP通过IP 获取 地理位置(实例代码)
  8. [转] 用source命令执行脚本和用sh执行脚本之间的区别
  9. Bootstrap--导航元素
  10. hdu2534-Score
  11. 第十五节,基本数据类型,元组tuple
  12. CF798 C. Mike and gcd problem
  13. MongoDB基础教程系列--未完待续
  14. dialog使用方法(同一页面,调用一个js代码,实现多个不同样式的弹窗)
  15. 图解如何安装MySQL5.0
  16. docker容器自动退出的问题
  17. 搭建一个简单的本地的dubbo-demo案例
  18. 基于SSH框架开发的《高校大学生选课系统》的质量属性的实现
  19. 为什么我们做分布式使用Redis?
  20. IntelliJ常用设置及快捷键

热门文章

  1. Python实例---抽屉后台框架分析
  2. Linux学习---Linux安装ftp组件
  3. UVa 1515 - Pool construction(最小割)
  4. element-ui : <el-table> 按钮点击操作阻止@row-click
  5. Python 多线程 使用线程 (二)
  6. .net增删该查DBAccess的应用
  7. PHP去重可用
  8. HDU 1162Eddy's picture(MST问题)
  9. MVC与MVVM之间在IOS中的区别
  10. shell习题第9题:sed的常用用法