kefu/tools/http.go

199 lines
4.4 KiB
Go
Raw Permalink Normal View History

2024-12-10 02:50:12 +00:00
package tools
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strings"
)
func GetStream(url string) (*bufio.Reader, io.ReadCloser, error) {
resp, err := http.Get(url)
if err != nil {
return nil, nil, err
}
reader := bufio.NewReader(resp.Body)
return reader, resp.Body, nil
}
func PostFormStream(url string, data url.Values) (*bufio.Reader, io.ReadCloser, error) {
resp, err := http.PostForm(url, data)
if err != nil {
return nil, nil, err
}
reader := bufio.NewReader(resp.Body)
return reader, resp.Body, nil
}
func Get(url string) string {
res, err := http.Get(url)
if err != nil {
log.Println(err)
return ""
}
var reader io.ReadCloser
if res.Header.Get("Content-Encoding") == "gzip" {
reader, err = gzip.NewReader(res.Body)
if err != nil {
return ""
}
} else {
reader = res.Body
}
//utf8Reader := transform.NewReader(reader,
// simplifiedchinese.GBK.NewDecoder())
robots, err := ioutil.ReadAll(reader)
res.Body.Close()
if err != nil {
log.Println(err)
return ""
}
return string(robots)
}
// Post("http://xxxx","application/json;charset=utf-8",[]byte("{'aaa':'bbb'}"))
func Post(url string, contentType string, body []byte) (string, error) {
if contentType == "" {
contentType = "application/x-www-form-urlencoded"
}
res, err := http.Post(url, contentType, strings.NewReader(string(body)))
if err != nil {
return "", err
}
defer res.Body.Close()
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(content), nil
}
func PostHeader(url string, msg []byte, headers map[string]string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(string(msg)))
if err != nil {
return "", err
}
for key, header := range headers {
req.Header.Set(key, header)
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func IsMobile(userAgent string) bool {
mobileRe, _ := regexp.Compile("(?i:Mobile|iPod|iPhone|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP)")
str := mobileRe.FindString(userAgent)
if str != "" {
return true
}
return false
}
func PostJson(url string, msg []byte) (string, error) {
headers := make(map[string]string)
headers["Content-Type"] = "application/json;charset=utf-8"
res, err := PostHeader(url, msg, headers)
return res, err
}
// PostJSON 发送HTTP POST请求并发送JSON数据
func PostJSON2(url string, jsonData interface{}) ([]byte, error) {
// 将JSON数据转换为字节切片
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
return nil, err
}
// 创建一个HTTP请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
// 发送HTTP请求并获取响应
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// 读取响应的内容
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBody, nil
}
// 发送http post请求数据为form
func PostForm(url string, data url.Values) (string, error) {
resp, err := http.PostForm(url, data)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(content), nil
}
// Get请求携带header
func GetWithHeader(url string, headers map[string]string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
for key, header := range headers {
req.Header.Set(key, header)
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// 获取当前访问的地址
func GetCurrentURL(r *http.Request) (Url string) {
scheme := "http://"
if r.TLS != nil {
scheme = "https://"
}
return strings.Join([]string{scheme, r.Host, r.RequestURI}, "")
}
// 获取当前访问的Host
func GetHost(r *http.Request) (Url string) {
scheme := "http://"
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
scheme = "https://"
}
return strings.Join([]string{scheme, r.Host}, "")
}