github.com/searKing/golang/go@v1.2.117/net/http/httpproxy/example_test.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package httpproxy_test
     6  
     7  import (
     8  	"log"
     9  	"net/http"
    10  
    11  	http_ "github.com/searKing/golang/go/net/http"
    12  	"github.com/searKing/golang/go/net/http/httpproxy"
    13  	_ "github.com/searKing/golang/go/net/resolver/passthrough"
    14  )
    15  
    16  func Example() {
    17  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    18  	proxy := &httpproxy.Proxy{
    19  		ProxyUrl:    "socks5://proxy.example.com:8080",
    20  		ProxyTarget: "127.0.0.1",
    21  	}
    22  	req = req.WithContext(httpproxy.WithProxy(req.Context(), proxy))
    23  
    24  	_, err := http_.DefaultTransportWithDynamicProxy.RoundTrip(req)
    25  	if err != nil {
    26  		log.Fatal(err)
    27  	}
    28  }