Go Resty 代理请求

20 min read
package main

import (
	"fmt"
	"gopkg.in/resty.v1"
	"os"
)

type App struct{}

func newDefaultRestyClient() *resty.Client {
	client := resty.New()
	client.SetDebug(os.Getenv("DEBUG") == "true")
	client.SetProxy("http://172.17.0.1:1081")
	client.SetLogger(os.Stdout)
	client.SetHostURL("https://www.v2ex.com")
	return client
}

func (app *App) SimpleGet(url string, respObj interface{}) error {
	resp, err := newDefaultRestyClient().R().
		SetHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9").
		SetHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36").
		SetHeader("Authentication", "None").
		SetResult(&respObj).
		Get(url)

	if err != nil {
		fmt.Fprintln(os.Stdout, resp.Body())
		SendMessage("v2ex定时任务执行失败")
	}

	return nil
}