mirror of
https://github.com/fumiama/go-nd-portal.git
synced 2026-06-13 05:04:26 +08:00
drop fb
This commit is contained in:
35
portal/web.go
Normal file
35
portal/web.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package portal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var client = &http.Client{}
|
||||
|
||||
// requestDataWith 使用自定义请求头获取数据
|
||||
func requestDataWith(url, method, ua string) (data []byte, err error) {
|
||||
// 提交请求
|
||||
var request *http.Request
|
||||
request, err = http.NewRequest(method, url, nil)
|
||||
if err == nil {
|
||||
// 增加header选项
|
||||
if ua != "" {
|
||||
request.Header.Add("User-Agent", ua)
|
||||
}
|
||||
var response *http.Response
|
||||
response, err = client.Do(request)
|
||||
if err == nil {
|
||||
if response.StatusCode != http.StatusOK {
|
||||
s := fmt.Sprintf("status code: %d", response.StatusCode)
|
||||
err = errors.New(s)
|
||||
return
|
||||
}
|
||||
data, err = io.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user