美文网首页树莓派
Go 语言学习系列002 -- 网络调用

Go 语言学习系列002 -- 网络调用

作者: 孙朗斌 | 来源:发表于2018-11-15 15:50 被阅读14次

    使用Go来实现网游的签到

    本篇文章,实际应用中并没有多大意义。不适用于所有网友及网游。只是笔者寓教于乐的一种形式。

    主要是学习和练习Go语言的以下知识点:

    • 变量的创建和赋值
    • 数组的创建、赋值和遍历
    • map的创建、修改和遍历
    • MD5加密
    • 网络请求
    • 打印

    [TOC]

    卡巴拉岛 畅玩服

    签到功能用的是网页实现。

    地址:

    [POST]:http://120.78.201.23:808/cShop/item_checkpt.asp

    [POST]:http://120.78.201.23:808/cShop/gacha.asp

    需要在Body中携带参数

    id=test1314
    key=e13efe12345656e3
    world=1
    

    其中:id是用户的账号名称,key是密码的16位小写MD5,world是服务器id。

    id=test1314&key=e13efe12345656e3&world=1
    

    简单实现

    package main
    
    import (
        "bytes"
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
    )
    
    func main() {
        sendRequest4()
    }
    
    func sendRequest4() {
        // Request (4) (POST http://120.78.201.23:808/cShop/gacha.asp)
    
        params := url.Values{}
        params.Set("id", "这里替换成自己的账号")
        params.Set("key", "这里替换成自己密码的小写16位MD5")
        params.Set("world", "1")
        body := bytes.NewBufferString(params.Encode())
    
        // Create client
        client := &http.Client{}
    
        // Create request
        req, err := http.NewRequest("POST", "http://120.78.201.23:808/cShop/gacha.asp", body)
    
        // Headers
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    
        // Fetch Request
        resp, err := client.Do(req)
    
        if err != nil {
            fmt.Println("Failure : ", err)
        }
    
        // Read Response Body
        respBody, _ := ioutil.ReadAll(resp.Body)
        
    
            // Display Results
        fmt.Println("response Status : ", resp.Status)
        fmt.Println("response Headers : ", resp.Header)
        fmt.Println("response Body : ", string(respBody))
    }
    
    

    变型封装

    将网络调用,抽象封装。把账号名和密码作为传参传递进功能函数。

    暂不考虑密码加密的问题,即已知加密后的密码。

    package main
    
    import (
        "bytes"
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
    )
    
    func main() {
        checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
        id := "这里替换成自己的账号"
        key := "这里替换成自己密码的小写16位MD5"
        sendRequest(checkptUrl,id,key)
    }
    
    func sendRequest(_url string,_id string,_key string) {
    
        params := url.Values{}
        params.Set("id", _id)
        params.Set("key", _key)
        params.Set("world", "1")
        body := bytes.NewBufferString(params.Encode())
    
        // Create client
        client := &http.Client{}
    
        // Create request
        req, err := http.NewRequest("POST", _url, body)
    
        // Headers
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    
        // Fetch Request
        resp, err := client.Do(req)
    
        if err != nil {
            fmt.Println("Failure : ", err)
        }
    
        // Read Response Body
        respBody, _ := ioutil.ReadAll(resp.Body)
    
        // Display Results
        fmt.Println("response Status : ", resp.Status)
        fmt.Println("response Body : ", string(respBody))
    }
    
    

    实现MD5加密

    实现32位小写的MD5

    func md5(str string) string {
        h := md5.New()
        h.Write([]byte(str))
        return hex.EncodeToString(h.Sum([]byte(nil)))
    }
    

    实现16位小写的MD5。就是把标准普通的MD5字符串从中截取了。

    func md5_16(str string) string {
        h := md5.New()
        h.Write([]byte(str))
        rs :=[]rune(hex.EncodeToString(h.Sum([]byte(nil))))
        return string(rs[8:24])
    }
    

    进一步改写

    通过用户名和密码(明文)进行功能的运行

    package main
    
    import (
        "bytes"
        "crypto/md5"
        "encoding/hex"
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
    )
    
    func main() {
        checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
        id := "这里替换成自己的账号"
        key := "这里替换成自己的密码"
        sendRequest(checkptUrl, id, md5_16(key))
    }
    
    func md5_16(str string) string {
        h := md5.New()
        h.Write([]byte(str))
        rs := []rune(hex.EncodeToString(h.Sum([]byte(nil))))
        return string(rs[8:24])
    }
    
    func sendRequest(_url string, _id string, _key string) {
        // Request (POST http://120.78.201.23:808/cShop/gacha.asp)
    
        params := url.Values{}
        params.Set("id", _id)
        params.Set("key", _key)
        params.Set("world", "1")
        body := bytes.NewBufferString(params.Encode())
    
        // Create client
        client := &http.Client{}
    
        // Create request
        req, err := http.NewRequest("POST", _url, body)
    
        // Headers
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    
        // Fetch Request
        resp, err := client.Do(req)
    
        if err != nil {
            fmt.Println("Failure : ", err)
        }
    
        // Read Response Body
        respBody, _ := ioutil.ReadAll(resp.Body)
    
        // Display Results
        fmt.Println("response Status : ", resp.Status)
        fmt.Println("response Body : ", string(respBody))
    }
    
    

    多账号

    使用数组来实现多账号列表。

    数组实现

    这里暂时没有对密码数组和账号数组的长度是否一直做判定,默认为长度相同。

    package main
    
    import (
        "bytes"
        "crypto/md5"
        "encoding/hex"
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
    )
    
    func main() {
        ids := [...]string{"账号1", "账号2", "账号3"}
        keys := [...]string{"账号1密码", "账号2密码", "账号3密码"}
        checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
        //gachaUrl := "http://120.78.201.23:808/cShop/gacha.asp"
    
        for index := 0; index < len(ids); index++ {
            sendRequest(checkptUrl, ids[index], md5_16(keys[index]))
        }
    
    }
    
    func sendRequest(_url string, _id string, _key string) {
        // Request (POST http://120.78.201.23:808/cShop/gacha.asp)
    
        params := url.Values{}
        params.Set("id", _id)
        params.Set("key", _key)
        params.Set("world", "1")
        body := bytes.NewBufferString(params.Encode())
    
        // Create client
        client := &http.Client{}
    
        // Create request
        req, err := http.NewRequest("POST", _url, body)
    
        // Headers
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    
        // Fetch Request
        resp, err := client.Do(req)
    
        if err != nil {
            fmt.Println("Failure : ", err)
        }
    
        // Read Response Body
        respBody, _ := ioutil.ReadAll(resp.Body)
    
        // Display Results
        fmt.Println("response Status : ", resp.Status)
        //fmt.Println("response Headers : ", resp.Header)
        fmt.Println("response Body : ", string(respBody))
    }
    
    func md5_16(str string) string {
        h := md5.New()
        h.Write([]byte(str))
        rs := []rune(hex.EncodeToString(h.Sum([]byte(nil))))
        return string(rs[8:24])
    }
    
    

    账号密码相同

    假如用户名和密码是相同的特殊情况。

    package main
    
    import (
        "bytes"
        "crypto/md5"
        "encoding/hex"
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
    )
    func main() {
    
        ids := [...]string{"账号1", "账号2", "账号3"}
        checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
        //gachaUrl := "http://120.78.201.23:808/cShop/gacha.asp"
    
        for _,value := range  ids {
            sendRequest(checkptUrl,value,md5_16(value))
        }
    
    }
    
    
    func sendRequest(_url string,_id string,_key string) {
        // Request (POST http://120.78.201.23:808/cShop/gacha.asp)
    
        params := url.Values{}
        params.Set("id", _id)
        params.Set("key", _key)
        params.Set("world", "1")
        body := bytes.NewBufferString(params.Encode())
    
        // Create client
        client := &http.Client{}
    
        // Create request
        req, err := http.NewRequest("POST", _url, body)
    
        // Headers
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    
        // Fetch Request
        resp, err := client.Do(req)
    
        if err != nil {
            fmt.Println("Failure : ", err)
        }
    
        // Read Response Body
        respBody, _ := ioutil.ReadAll(resp.Body)
    
        // Display Results
        fmt.Println("response Status : ", resp.Status)
        //fmt.Println("response Headers : ", resp.Header)
        fmt.Println("response Body : ", string(respBody))
    }
    
    func md5_16(str string) string {
        h := md5.New()
        h.Write([]byte(str))
        rs :=[]rune(hex.EncodeToString(h.Sum([]byte(nil))))
        return string(rs[8:24])
    }
    

    map实现

    package main
    
    import (
        "bytes"
        "crypto/md5"
        "encoding/hex"
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
    )
    func main() {
    
        checkptUrl := "http://120.78.201.23:808/cShop/item_checkpt.asp"
        //gachaUrl := "http://120.78.201.23:808/cShop/gacha.asp"
        
        map_id := make(map[string]string)
        map_id["账号1"] = md5_16("密码1")
        map_id["账号2"] = md5_16("密码2")
        map_id["账号3"] = md5_16("密码3")
    
        for key,value := range  ids {
            sendRequest(checkptUrl,key,value)
        }
    
    }
    
    
    func sendRequest(_url string,_id string,_key string) {
        // Request (POST http://120.78.201.23:808/cShop/gacha.asp)
    
        params := url.Values{}
        params.Set("id", _id)
        params.Set("key", _key)
        params.Set("world", "1")
        body := bytes.NewBufferString(params.Encode())
    
        // Create client
        client := &http.Client{}
    
        // Create request
        req, err := http.NewRequest("POST", _url, body)
    
        // Headers
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    
        // Fetch Request
        resp, err := client.Do(req)
    
        if err != nil {
            fmt.Println("Failure : ", err)
        }
    
        // Read Response Body
        respBody, _ := ioutil.ReadAll(resp.Body)
    
        // Display Results
        fmt.Println("response Status : ", resp.Status)
        //fmt.Println("response Headers : ", resp.Header)
        fmt.Println("response Body : ", string(respBody))
    }
    
    func md5_16(str string) string {
        h := md5.New()
        h.Write([]byte(str))
        rs :=[]rune(hex.EncodeToString(h.Sum([]byte(nil))))
        return string(rs[8:24])
    }
    

    相关文章

      网友评论

        本文标题:Go 语言学习系列002 -- 网络调用

        本文链接:https://www.haomeiwen.com/subject/ncvyfqtx.html