golang 通过代理 post 代码片段

package main

import (
	"fmt"
	"net/http"
	"net/url"
	"strings"
)

func main() {
	httpDo()
}

func httpDo() {

	proxy := func(_ *http.Request) (*url.URL, error) {
		return url.Parse("http://120.78.78.141:8888")
	}

	transport := &http.Transport{Proxy: proxy}

	client := &http.Client{Transport: transport}
	var r http.Request
	r.ParseForm()
	r.Form.Add("uuid", "12311")
	bodystr := strings.TrimSpace(r.Form.Encode())
	req, err := http.NewRequest("POST", "http://www.coffiasd.cn/app/test_proxy", strings.NewReader(bodystr))
	if err != nil {
		// handle error
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("User-Agent", "client-sn22")

	resp, err := client.Do(req)

	defer resp.Body.Close()

	fmt.Println("Http reurened stats %v", resp.Status)
}

  

猜你喜欢

转载自www.cnblogs.com/coffiasd/p/9155498.html