代码拉取完成,页面将自动刷新
package jump
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
)
const DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.59 Safari/537.36"
func NewRequest() *Request {
jar, _ := cookiejar.New(nil)
return &Request{
Headers: map[string]string{
"User-Agent": DefaultUserAgent,
},
client: &http.Client{
Timeout: time.Second * 30,
Jar: jar,
},
Jar: jar,
}
}
type Request struct {
client *http.Client
Headers map[string]string
Jar *cookiejar.Jar
req http.Request
}
func (r *Request) Do(method, uri string, headers map[string]string, body io.Reader) (*http.Response, []byte, error) {
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, nil, err
}
for key, value := range r.Headers {
req.Header.Set(key, value)
}
for key, value := range headers {
req.Header.Set(key, value)
}
resp, err := r.client.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
return resp, respBody, err
}
func (r *Request) Get(uri string) (*http.Response, []byte, error) {
return r.Do("GET", uri, nil, nil)
}
func (r *Request) Post(uri string, headers map[string]string, body io.Reader) (*http.Response, []byte, error) {
return r.Do("POST", uri, headers, body)
}
func (r *Request) PostJSON(uri string, data map[string]interface{}) (*http.Response, []byte, error) {
jsonValue, _ := json.Marshal(data)
return r.Do("POST", uri, map[string]string{
"Content-Type": "application/json; charset=utf-8",
}, bytes.NewBuffer(jsonValue))
}
func (r *Request) PostForm(uri string, data map[string]string) (*http.Response, []byte, error) {
form := url.Values{}
for key, value := range data {
form.Add(key, value)
}
return r.Do("POST", uri, map[string]string{
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
}, strings.NewReader(form.Encode()))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。