文章详情
接入指南 - 代码 - Go
package main

import (
    "net/url"
    "net/http"
    "bytes"
    "fmt"
    "io/ioutil"
)

// 代理服务器
const proxyServer = "tunnel.quqiquqi.com:8600"

// 代理隧道验证信息
const proxyUser  = "U123456789";
const proxyPass  = "P123456789";

type QuqiProxy struct {
    AppID string
    AppSecret string
}

func (p QuqiProxy) ProxyClient() http.Client {
    proxyUrl, _ := url.Parse("http://"+ p.AppID +":"+ p.AppSecret +"@"+ proxyServer)
    return http.Client{Transport: &http.Transport{
    Proxy: http.ProxyURL(proxyUrl),
    DisableKeepAlives: true,
    }}
}

func main()  {
    targetUrl := "http://www.quqiquqi.com/test"
    // 请求切换IP(仅短效版、长效版用。动态版每次请求自动切换,无需指定)
    //targetUrl := "https://www.quqiquqi.com/switch-ip"
    // 查看当前隧道出口IP信息
    //targetUrl := "https://www.quqiquqi.com/current-ip"

    // 初始化 proxy http client
    client := QuqiProxy{AppID: proxyUser, AppSecret: proxyPass}.ProxyClient()

    request, _ := http.NewRequest("GET", targetUrl, bytes.NewBuffer([]byte(``)))

    // 设置IP切换头(仅支持HTTP)
    // 仅短效版、长效版用,动态版每次请求自动切换,无需指定。
    request.Header.Set("Proxy-Switch-Ip", "yes")

    response, err := client.Do(request)

    if err != nil {
        panic("failed to connect: " + err.Error())
    } else {
        bodyByte, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Println("读取 Body 时出错", err)
            return
        }
        response.Body.Close()

        body := string(bodyByte)

        fmt.Println("Response Status:", response.Status)
        fmt.Println("Response Header:", response.Header)
        fmt.Println("Response Body:\n", body)
    }
}
发布时间:2021-10-07

在线客服