go ---- 模仿python的requests库
package libs
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"time"
)
type xyResponse struct {
Status int
Text string
Url string
}
//三目运算
func If(condition bool, trueVal, falseVal interface{}) interface{} {
if condition {
return trueVal
}
return falseVal
}
func determineEncoding(r *bufio.Reader) encoding.Encoding {
bytes, err := r.Peek(1024)
if err != nil {
log.Printf("Fetcher error: %v", err)
return unicode.UTF8
}
e, _, _ := charset.DetermineEncoding(bytes, "")
return e
}
func HttpRequest(method string, httpUrl string, headers map[string]string, httpParams map[string]string,
httpData map[string]string) xyResponse {
client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
result := xyResponse{}
params := url.Values{}
Url, err := url.Parse(httpUrl)
if len(httpParams) > 0 {
for key, val := range httpParams {
params.Set(key, val)
}
}
Url.RawQuery = params.Encode()
urlPath := Url.String()
result.Url = urlPath
var req *http.Request
if httpData != nil {
dataJson,_ :=json.Marshal(httpData)
req, _ = http.NewRequest(method, urlPath, bytes.NewBuffer(dataJson))
} else {
req, _ = http.NewRequest(method, urlPath, nil)
}
if len(headers) > 0 {
for key, val := range headers {
req.Header.Set(key, val)
}
}
// 发起请求
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return result
}
defer resp.Body.Close()
result.Status = resp.StatusCode
if err != nil {
fmt.Errorf("err")
return result
}
bodyReader := bufio.NewReader(resp.Body)
e := determineEncoding(bodyReader)
utf8Reader := transform.NewReader(bodyReader, e.NewDecoder())
body, _ := ioutil.ReadAll(utf8Reader)
result.Text = string(body)
//fmt.Println(client.)
return result
}
测试:
func TestGet() {
headers := map[string]string{"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"}
url := "http://www.tianqi.com/chinacity.html"
page := libs.HttpRequest("GET", url, headers, nil, nil)
//fmt.Println(len(page))
fmt.Println(page.Text)
}
func TestPost() {
headers := map[string]string{"Content-Type": "application/json;charset=UTF-8"}
url := "http://127.0.0.1:5555/test"
data := map[string]string{"name" : "bigdataboy", "description": "ttt"}
page := libs.HttpRequest("POST", url, headers, nil, data)
fmt.Println("============>", page.Text)
}
夕阳的进步与发展,靠各位同仁的支持与鼓励!谢谢!!!!

浙公网安备 33010602011771号