添加代码
This commit is contained in:
39
network.go
Normal file
39
network.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Client *http.Client
|
||||
|
||||
func init() {
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport.MaxIdleConnsPerHost = 10
|
||||
jar, _ := cookiejar.New(nil)
|
||||
Client = &http.Client{
|
||||
Transport: transport,
|
||||
CheckRedirect: nil,
|
||||
Jar: jar,
|
||||
Timeout: 15 * time.Second,
|
||||
}
|
||||
}
|
||||
func createRequest(method string, url string, body io.Reader) *http.Request {
|
||||
req, _ := http.NewRequest(method, url, body)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0")
|
||||
return req
|
||||
}
|
||||
func checkHttpError(res *http.Response, err error) error {
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return errors.New(strconv.Itoa(res.StatusCode))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user