sdu-auto-evaluation/network.go
2021-12-22 22:28:40 +08:00

40 行
962 B
Go

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
}