新建一个请求
(method, urls string)
| 72 | |
| 73 | // 新建一个请求 |
| 74 | func (h *Http) New(method, urls string) *Http { |
| 75 | var err error |
| 76 | h.err = nil |
| 77 | h.Ctx, h.CtxCancel = context.WithCancel(context.Background()) |
| 78 | h.HttpRequestUrl = urls |
| 79 | h.HttpRequestType = method |
| 80 | // 初始化http client 如果开启了session,则传入cookie jar |
| 81 | if h.HttpTransport == nil { |
| 82 | //log.Println("new transport") |
| 83 | h.HttpTransport = &transport |
| 84 | } |
| 85 | h.HttpClient = &http.Client{Transport: h.HttpTransport} |
| 86 | |
| 87 | if h.isSession { |
| 88 | if h.Cookie == nil { |
| 89 | h.Cookie, _ = cookiejar.New(nil) |
| 90 | } |
| 91 | if h.HttpClient.Jar == nil { |
| 92 | h.HttpClient.Jar = h.Cookie |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | h.SetTimeOut(30) |
| 97 | h.IgnoreSSL() |
| 98 | h.HttpRequest, err = http.NewRequest(h.HttpRequestType, h.HttpRequestUrl, h.HttpBody) |
| 99 | h.HttpRequest.WithContext(h.Ctx) |
| 100 | h.err = err |
| 101 | return h |
| 102 | // return err |
| 103 | } |
| 104 | func (h *Http) SetTimeOut(t int) { |
| 105 | td := time.Duration(t) |
| 106 | h.HttpTransport.TLSHandshakeTimeout = td * time.Second |