设为首页 加入收藏

TOP

Go实现发送解析GET与POST请求(一)
2018-10-19 15:51:00 】 浏览:106
Tags:实现 发送 解析 GET POST 请求

参考链接:

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"][0] == "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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Go Web 使用工具 下一篇Go语言_iota用法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目