跳至主要內容

Golang

小于 1 分钟

Golang

标准http


package main

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

// Proxy tunnel verification information
const (
	proxyHost            = "malenia.iinti.cn"
	proxyPort            = 24000
	proxyUser, proxyPass = "yourProxyAccount", "yourProxyPassword"
	targetUrl            = `http://myip.ipip.net/`
	maxWaitTime          = 10
)

// ProxyTransport Custom proxy connection Transport
func ProxyClient() *http.Transport {
	proxyMate := fmt.Sprintf("http://%s:%s@%s:%d", proxyUser, proxyPass, proxyHost, proxyPort)
	proxyUrl, err := url.Parse(proxyMate)
	if err != nil {
		fmt.Println(`Parse proxyMate while error`, err)
	}
	return &http.Transport{
		Proxy: http.ProxyURL(proxyUrl),
	}
}

func main() {
	client := &http.Client{Transport: ProxyClient()}
	request, _ := http.NewRequest("GET", targetUrl, bytes.NewBuffer([]byte(``)))
	if response, err := client.Do(request); err != nil {
		fmt.Println("failed to connect: " + err.Error())
	} else {
		bodyByte, err := ioutil.ReadAll(response.Body)
		if err != nil {
			fmt.Println("Error reading body:", err)
			return
		}
		response.Body.Close()
		body := string(bodyByte)
		fmt.Println("Response Body:", body)
	}
}

colly

package main

import (
	"fmt"
	"github.com/gocolly/colly"
	"net/http"
	"net/url"
)

// Proxy tunnel verification information
const (
	proxyHost            = "malenia.iinti.cn"
	proxyPort            = 24000
	proxyUser, proxyPass = "yourProxyAccount", "yourProxyPassword"
	targetUrl            = `http://myip.ipip.net/`
	maxWaitTime          = 10
)

// ProxyTransport Custom proxy connection Transport
func ProxyTransport() *http.Transport {
	proxyMate := fmt.Sprintf("http://%s:%s@%s:%d", proxyUser, proxyPass, proxyHost, proxyPort)
	proxyUrl, err := url.Parse(proxyMate)
	if err != nil {
		fmt.Println(`Parse proxyMate while error`, err)
	}
	return &http.Transport{
		Proxy: http.ProxyURL(proxyUrl),
	}
}

func main() {
	// Instantiate default collector
	c := colly.NewCollector(
		// Visit only domains: hackerspaces.org, wiki.hackerspaces.org
		colly.AllowedDomains("hackerspaces.org", "wiki.hackerspaces.org"),
	)
	// 使用自定义Transport ProxyTransport 对接代理
	c.WithTransport(ProxyTransport())

	// On every a element which has href attribute call callback
	c.OnHTML("a[href]", func(e *colly.HTMLElement) {
		link := e.Attr("href")
		// Print link
		fmt.Printf("Link found: %q -> %s\n", e.Text, link)
		// Visit link found on page
		// Only those links are visited which are in AllowedDomains
		c.Visit(e.Request.AbsoluteURL(link))
	})

	// Before making a request print "Visiting ..."
	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL.String())
	})
}

上次编辑于:
贡献者: iinti_cn