欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

requests

程序员文章站 2022-05-07 10:56:49
...
package main

import (
    "net/http"
    "bytes"
    "strconv"
    "regexp"
    "golang.org/x/text/transform"
    "golang.org/x/text/encoding/simplifiedchinese"
    "io/ioutil"
)

var(
    handlerErr = Error{}
)

type Requests struct {}

func (r Requests) Get(url string) string{
    client := http.Client{}
    request,err := http.NewRequest("GET", url, nil)
    handlerErr.logFatal(err)
    response, err := client.Do(request)
    handlerErr.logFatal(err)
    if response.StatusCode == 200{
        buf := new(bytes.Buffer)
        buf.ReadFrom(response.Body)
        return buf.String()
    }else {
        return strconv.Itoa(response.StatusCode)
    }
}

func (r Requests) gbk2utf8(s []byte)([]byte, error){
    reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
    d, e := ioutil.ReadAll(reader)
    if e != nil {
        return nil, e
    }
    return d, nil
}

func (r Requests) gbk2utf8Str(s string)(string, error){
    b,err := r.gbk2utf8(r.str2byte(s))
    return string(b),err
}

func (r Requests) str2byte(s string)[]byte{
    return []byte(s)
}

func (r Requests) findAllSubMatch(target string, pattern *regexp.Regexp) [][]string{
    return pattern.FindAllStringSubmatch(target, -1)
}

上一篇: 9.20任务

下一篇: 【第4章】4.4任务