You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.1 KiB

2 years ago
package xhttp
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const timeout = time.Second * 120
func Request(method string, reqUrl string, postData string, header http.Header) (string, error) {
var res string
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
Timeout: timeout,
}
var ioreader io.Reader
if postData != "" {
ioreader = strings.NewReader(postData)
}
req, err := http.NewRequest(method, reqUrl, ioreader)
if err != nil {
return res, err
}
if header != nil {
req.Header = header
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
byt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
return string(byt), nil
} else {
return res, fmt.Errorf("code:%d message:%s", resp.StatusCode, resp.Status)
}
}
func Get(reqUrl string, timeout time.Duration) (string, error) {
var res string
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
Timeout: timeout,
}
req, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
return res, err
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
byt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
return string(byt), nil
} else {
return res, fmt.Errorf("code:%d message:%s", resp.StatusCode, resp.Status)
}
}
func fetcher(method, u, post string, headers map[string]string, timeout int64) (*[]byte, error) {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
Timeout: time.Second * time.Duration(timeout),
}
req, err := http.NewRequest(method, u, strings.NewReader(post))
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Add(k, v)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("FetchUrl status:%s", resp.Status)
}
return &body, err
}
func PostForm(u, post string, headers map[string]string) (*[]byte, error) {
if headers == nil {
headers = make(map[string]string)
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return fetcher("POST", u, post, headers, 60)
}
func PostJson(u, post string, headers map[string]string) (*[]byte, error) {
if headers == nil {
headers = make(map[string]string)
}
headers["Content-Type"] = "application/json;charset=UTF-8"
fmt.Printf("%+v\n", post)
fmt.Printf("%+v\n", headers)
return fetcher("POST", u, post, headers, 60)
}
func GetWithHeader(u string, headers map[string]string) (*[]byte, error) {
if headers == nil {
headers = make(map[string]string)
}
return fetcher("GET", u, "", headers, 60)
}
type apiRes struct {
Status int64 `json:"status"`
}
func SenEmail(u string, data map[string]string) error {
fmt.Println(u)
postByte, _ := json.Marshal(data)
fmt.Printf("%s\n", postByte)
values := url.Values{}
for k, v := range data {
values.Add(k, v)
}
fmt.Println(data)
res, err := PostForm(u, values.Encode(), nil)
if err != nil {
return err
}
fmt.Printf("%s\n", *res)
result := apiRes{}
if err := json.Unmarshal(*res, &result); err != nil {
return err
}
if result.Status != 200 {
return errors.New(string(*res))
}
return nil
}
type shotResp struct {
Code int64 `json:"code"`
Info string `json:"info"`
Message string `json:"message"`
}
func ShotUrl(u string) (string, error) {
values := url.Values{}
values.Add("long_url", u)
res, err := PostForm(`https://echinajobs.cn/add`, values.Encode(), nil)
if err != nil {
return "", err
}
result := shotResp{}
if err := json.Unmarshal(*res, &result); err != nil {
return "", err
}
if result.Code != 200 {
return "", errors.New(string(*res))
}
return result.Info, nil
}