当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
5 Star 17 Fork 6

prettyyjnic/youjumpijump
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
request.go 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
prettyyjnic 提交于 2018-01-01 23:06 . update
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()))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/stuinfer/youjumpijump.git
git@gitee.com:stuinfer/youjumpijump.git
stuinfer
youjumpijump
youjumpijump
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385