http.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package lxhttp
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "crypto/tls"
  6. "encoding/base64"
  7. "encoding/json"
  8. "io"
  9. "net/http"
  10. "net/http/cookiejar"
  11. "net/url"
  12. "strings"
  13. "time"
  14. "git.listensoft.net/tool/jspkit/taxerr"
  15. "golang.org/x/net/publicsuffix"
  16. )
  17. // POSTForm 发送Form表单请求
  18. func POSTForm(c *http.Client, requrl string, params map[string]string, headers map[string]string) ([]byte, error) {
  19. form := url.Values{}
  20. for k, v := range params {
  21. form.Add(k, v)
  22. }
  23. var bys []byte
  24. req, err := http.NewRequest("POST", requrl, strings.NewReader(form.Encode()))
  25. if err != nil {
  26. return bys, err
  27. }
  28. for k, v := range headers {
  29. req.Header.Add(k, v)
  30. }
  31. resp, err := c.Do(req)
  32. if err != nil {
  33. return bys, err
  34. }
  35. defer resp.Body.Close()
  36. return io.ReadAll(resp.Body)
  37. }
  38. // GET 发送get请求
  39. func GET(c *http.Client, requrl string, params map[string]string, headers map[string]string) ([]byte, error) {
  40. form := url.Values{}
  41. for k, v := range params {
  42. form.Add(k, v)
  43. }
  44. var bys []byte
  45. req, err := http.NewRequest("GET", requrl, strings.NewReader(form.Encode()))
  46. if err != nil {
  47. return bys, err
  48. }
  49. for k, v := range headers {
  50. req.Header.Add(k, v)
  51. }
  52. resp, err := c.Do(req)
  53. if err != nil {
  54. return bys, err
  55. }
  56. defer resp.Body.Close()
  57. return io.ReadAll(resp.Body)
  58. }
  59. func GZIPDecode(in []byte) ([]byte, error) {
  60. reader, err := gzip.NewReader(bytes.NewReader(in))
  61. if err != nil {
  62. var out []byte
  63. return out, err
  64. }
  65. defer reader.Close()
  66. return io.ReadAll(reader)
  67. }
  68. // POSTJson 发送json请求
  69. func POSTJson1(c *http.Client, requrl string, params map[string]any, headers map[string]string) ([]byte, error) {
  70. bytesData, _ := json.Marshal(params)
  71. request, err := http.NewRequest("POST", requrl, bytes.NewReader(bytesData))
  72. if err != nil {
  73. return nil, err
  74. }
  75. for k, v := range headers {
  76. request.Header.Add(k, v)
  77. }
  78. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  79. request.Header.Set("Accept-Encoding", "gzip, deflate, br")
  80. resp, err := c.Do(request)
  81. if err != nil {
  82. return nil, err
  83. }
  84. defer resp.Body.Close()
  85. bytes, err := io.ReadAll(resp.Body)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if strings.Contains(resp.Header.Get(`Content-Encoding`), "gzip") {
  90. return GZIPDecode(bytes)
  91. }
  92. return bytes, nil
  93. }
  94. // POSTJson 发送json请求
  95. func POSTJsonAny(c *http.Client, requrl string, params any, headers map[string]string) ([]byte, error) {
  96. bytesData, _ := json.Marshal(params)
  97. request, err := http.NewRequest("POST", requrl, bytes.NewReader(bytesData))
  98. if err != nil {
  99. return nil, err
  100. }
  101. for k, v := range headers {
  102. request.Header.Add(k, v)
  103. }
  104. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  105. request.Header.Set("Accept-Encoding", "gzip, deflate, br")
  106. resp, err := c.Do(request)
  107. if err != nil {
  108. return nil, err
  109. }
  110. defer resp.Body.Close()
  111. bytes, err := io.ReadAll(resp.Body)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if strings.Contains(resp.Header.Get(`Content-Encoding`), "gzip") {
  116. return GZIPDecode(bytes)
  117. }
  118. return bytes, nil
  119. }
  120. // POSTJson 发送json请求
  121. func POSTStrReader(c *http.Client, requrl, params string, headers map[string]string) ([]byte, error) {
  122. request, err := http.NewRequest("POST", requrl, strings.NewReader(params))
  123. if err != nil {
  124. return nil, err
  125. }
  126. for k, v := range headers {
  127. request.Header.Add(k, v)
  128. }
  129. request.Header.Set("Accept-Encoding", "gzip, deflate, br")
  130. resp, err := c.Do(request)
  131. if err != nil {
  132. return nil, err
  133. }
  134. defer resp.Body.Close()
  135. bytes, err := io.ReadAll(resp.Body)
  136. if err != nil {
  137. return nil, err
  138. }
  139. if strings.Contains(resp.Header.Get(`Content-Encoding`), "gzip") {
  140. return GZIPDecode(bytes)
  141. }
  142. return bytes, nil
  143. }
  144. // POSTJson 发送json请求
  145. func POSTJson(c *http.Client, requrl string, params map[string]string, headers map[string]string) ([]byte, error) {
  146. bytesData, _ := json.Marshal(params)
  147. request, err := http.NewRequest("POST", requrl, bytes.NewReader(bytesData))
  148. if err != nil {
  149. return nil, err
  150. }
  151. for k, v := range headers {
  152. request.Header.Add(k, v)
  153. }
  154. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  155. request.Header.Set("Accept-Encoding", "gzip, deflate, br")
  156. resp, err := c.Do(request)
  157. if err != nil {
  158. return nil, err
  159. }
  160. if resp.StatusCode == 504 && strings.Contains(requrl, "keepalive") { // 超时
  161. return nil, taxerr.KeepTimeOut
  162. }
  163. defer resp.Body.Close()
  164. bytes, err := io.ReadAll(resp.Body)
  165. if err != nil {
  166. return nil, err
  167. }
  168. if strings.Contains(resp.Header.Get(`Content-Encoding`), "gzip") {
  169. return GZIPDecode(bytes)
  170. }
  171. return bytes, nil
  172. }
  173. // Get http get请求
  174. func Get(c *http.Client, url string) ([]byte, error) {
  175. resp, err := c.Get(url)
  176. if err != nil {
  177. return nil, err
  178. }
  179. defer resp.Body.Close()
  180. return io.ReadAll(resp.Body)
  181. }
  182. // NewHttpClient 生成一个htppclient
  183. func NewHttpClient() *http.Client {
  184. tr := &http.Transport{
  185. // Proxy: http.ProxyFromEnvironment, // 从环境变量中获取代理
  186. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 忽略证书校验错误
  187. }
  188. var c = &http.Client{
  189. Timeout: time.Duration(60 * time.Second),
  190. Transport: tr,
  191. }
  192. options := cookiejar.Options{
  193. PublicSuffixList: publicsuffix.List,
  194. }
  195. curCookieJar, _ := cookiejar.New(&options)
  196. c.Jar = curCookieJar
  197. return c
  198. }
  199. // NewHttpClient 生成一个htppclient 请求等待时间加长
  200. func NewLongHttpClient() *http.Client {
  201. tr := &http.Transport{
  202. // Proxy: http.ProxyFromEnvironment, // 从环境变量中获取代理
  203. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 忽略证书校验错误
  204. }
  205. var c = &http.Client{
  206. Timeout: time.Duration(600 * time.Second),
  207. Transport: tr,
  208. }
  209. options := cookiejar.Options{
  210. PublicSuffixList: publicsuffix.List,
  211. }
  212. curCookieJar, _ := cookiejar.New(&options)
  213. c.Jar = curCookieJar
  214. return c
  215. }
  216. // NewHttpClient 生成一个代理htppclient
  217. func NewHttpClientForProxy(proxy string) *http.Client {
  218. var tr *http.Transport
  219. if proxy == "" {
  220. tr = &http.Transport{
  221. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //忽略证书校验
  222. }
  223. } else {
  224. p, _ := url.Parse(proxy)
  225. tr = &http.Transport{
  226. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //忽略证书校验
  227. Proxy: http.ProxyURL(p),
  228. }
  229. }
  230. var c = &http.Client{
  231. Timeout: time.Duration(20 * time.Second),
  232. Transport: tr,
  233. }
  234. options := cookiejar.Options{
  235. PublicSuffixList: publicsuffix.List,
  236. }
  237. curCookieJar, _ := cookiejar.New(&options)
  238. c.Jar = curCookieJar
  239. return c
  240. }
  241. // NewHttpClient 生成一个待认证的htppclient (仅适用西藏)
  242. func NewHttpClientForXizang(ip, auth string) *http.Client {
  243. p, _ := url.Parse(ip)
  244. tr := &http.Transport{
  245. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //忽略证书校验
  246. Proxy: http.ProxyURL(p),
  247. }
  248. if auth != "" {
  249. tr.ProxyConnectHeader = http.Header{
  250. "Proxy-Authorization": []string{"Basic " + base64.StdEncoding.EncodeToString([]byte(auth))},
  251. }
  252. }
  253. var c = &http.Client{
  254. Timeout: time.Duration(20 * time.Second),
  255. Transport: tr,
  256. }
  257. options := cookiejar.Options{
  258. PublicSuffixList: publicsuffix.List,
  259. }
  260. curCookieJar, _ := cookiejar.New(&options)
  261. c.Jar = curCookieJar
  262. return c
  263. }
  264. // NewHttpClientForLocalProxy 针对本地代理 需要账号认证的 不认证的也可以用,后面全用这个
  265. func NewHttpClientForLocalProxy(ip, auth string) *http.Client {
  266. if ip == "" {
  267. return NewHttpClient()
  268. }
  269. p, _ := url.Parse(ip)
  270. tr := &http.Transport{
  271. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //忽略证书校验
  272. Proxy: http.ProxyURL(p),
  273. }
  274. if auth != "" {
  275. tr.ProxyConnectHeader = http.Header{
  276. "Proxy-Authorization": []string{"Basic " + base64.StdEncoding.EncodeToString([]byte(auth))},
  277. }
  278. }
  279. var c = &http.Client{
  280. Timeout: time.Duration(60 * time.Second),
  281. Transport: tr,
  282. }
  283. options := cookiejar.Options{
  284. PublicSuffixList: publicsuffix.List,
  285. }
  286. curCookieJar, _ := cookiejar.New(&options)
  287. c.Jar = curCookieJar
  288. return c
  289. }
  290. // CreateFormReader 将 map 转换成 header
  291. func CreateFormReader(data map[string]string) io.Reader {
  292. form := url.Values{}
  293. for k, v := range data {
  294. form.Add(k, v)
  295. }
  296. return strings.NewReader(form.Encode())
  297. }
  298. func HttpClientPost(client *http.Client, URL, contentType string, body io.Reader) (res []byte, err error) {
  299. resp, err := client.Post(URL, contentType, body)
  300. if err != nil {
  301. return
  302. }
  303. defer resp.Body.Close()
  304. return io.ReadAll(resp.Body)
  305. }