使用GoLang通过API提取链接获取代理IP的代码样例
站大爷
官方
2024-01-20
1506 浏览
温馨提示:
1. 站大爷API提取链接在控制台中"实例管理"中生成
package main
//GO版本 GO1
import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
func main() {
// api链接
api_url := "http://www.***.com/ShortProxy/GetIP/?api=1234567890&akey=8a17ca305f683620&count=10×pan=3&type=3"
// 请求api链接
req, _ := http.NewRequest("GET", api_url, nil)
req.Header.Add("Accept-Encoding", "gzip") //使用gzip压缩传输数据让访问更快
client := &http.Client{}
res, err := client.Do(req)
// 处理返回结果
if err != nil {
// 请求发生异常
fmt.Println(err.Error())
} else {
defer res.Body.Close() //保证最后关闭Body
fmt.Println("status code:", res.StatusCode) // 获取状态码
// 有gzip压缩时,需要解压缩读取返回内容
if res.Header.Get("Content-Encoding") == "gzip" {
reader, _ := gzip.NewReader(res.Body) // gzip解压缩
defer reader.Close()
io.Copy(os.Stdout, reader)
os.Exit(0) // 正常退出
}
// 无gzip压缩, 读取返回内容
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
}