Browse Source

请求添加代理

pull/260/head
zhangxin 3 years ago
parent
commit
25cec9c8d7
1 changed files with 16 additions and 8 deletions
  1. +16
    -8
      utils/request.go

+ 16
- 8
utils/request.go View File

@ -1,20 +1,24 @@
package utils
import (
"LAPP_GAAS_GFrame_BACKEND/conf"
"bytes"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"time"
)
func GetRequest(url string, headers map[string]string) (body []byte, err error) {
func GetRequest(u string, headers map[string]string) (body []byte, err error) {
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(conf.DbConfig.Proxy)
}
// 超时时间:5秒
client := &http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest(http.MethodGet, url, nil)
transport := &http.Transport{Proxy: proxy}
client := &http.Client{Transport: transport}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
@ -34,8 +38,12 @@ func GetRequest(url string, headers map[string]string) (body []byte, err error)
}
func PostFormDataWithSingleFile(url string, filepath string, filename string, headers map[string]string) (body []byte, err error){
client := http.Client{}
func PostFormDataWithSingleFile(u string, filepath string, filename string, headers map[string]string) (body []byte, err error){
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(conf.DbConfig.Proxy)
}
transport := &http.Transport{Proxy: proxy}
client := &http.Client{Transport: transport}
bodyBuf := &bytes.Buffer{}
bodyWrite := multipart.NewWriter(bodyBuf)
file, err := os.Open(filepath)
@ -59,7 +67,7 @@ func PostFormDataWithSingleFile(url string, filepath string, filename string, he
}
// 创建请求
contentType := bodyWrite.FormDataContentType()
req, err := http.NewRequest(http.MethodPost, url, bodyBuf)
req, err := http.NewRequest(http.MethodPost, u, bodyBuf)
if err != nil {
return nil, err
}


Loading…
Cancel
Save