package main
import (
"bytes"
"errors"
"fmt"
"net/http"
"strings"
)
func redirectPolicyFunc(req *http.Request, via []*http.Request) error {
if len(via) >= 1{
return errors.New("stopped after 1 redirects")
}
return nil
}
func GetImageurl(api string)string{
client := &http.Client{
CheckRedirect: redirectPolicyFunc,
}
reqest,_:=http.NewRequest("GET",api,nil)
//reqest.Header.Add("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
resp, _ := client.Do(reqest)
defer resp.Body.Close()
url,_:=resp.Location()
return url.String()
}
func GetImagedata(imageurl string)string{
/*client := &http.Client{
CheckRedirect: redirectPolicyFunc,
}
reqest,_:=http.NewRequest("GET","api",nil)
reqest.Header.Add("Accept-Language","zh-CN")
reqest.Header.Add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763")
//reqest.Header.Del()
reqest.Header.Add("Upgrade-Insecure-Requests","1")
reqest.Header.Add("Connection","close")
reqest.Header.Add("Accept-Encoding","gzip, deflate")
reqest.Header.Add("Cookie","security_session_verify=93344b72152e59dffb9233be1d328dc1")
resp, _ := client.Do(reqest)
//reqs[0].Close=true
fmt.Println(resp.Location())
defer resp.Body.Close()
*/
fmt.Println(imageurl)
resp,_:=http.Get(imageurl)
defer resp.Body.Close()
/*f, err := os.Create("xxs"+".jpg")
if err != nil {
panic(err)
}
io.Copy(f, resp.Body)*/
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
s := buf.String()
return s
//fmt.Println(string(buf))
}
func sayapiurl(w http.ResponseWriter, r *http.Request) {
url:=r.URL.Path
if url!="/favicon.ico" {
tmp:=strings.SplitN(url,"/",3)
tmp2:=tmp[1]+`://`
tmp2+=tmp[2]
fmt.Fprint(w,GetImagedata(GetImageurl(tmp2)))
}
}
func main() {
http.HandleFunc("/", sayapiurl) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
fmt.Println("ListenAndServe: ", err)
}
}
格式http://ip:9090/https/api地址(不带https://)