인증과 함께 http 프록시로 이동
import ( "net/http" "net/url" ) // Create a proxy URL with authentication proxyURL := &url.URL{ Scheme: "http", User: url.UserPassword("username", "password"), Host: "proxy.com:8080", } // Create a custom HTTP client with the proxy client := &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyURL(proxyURL), }, } // Use the custom client with the third-party package resp, err := client.PostForm(method, params) if err != nil { // Handle error }
import ( "net/http" "net/url" ) // Create a proxy URL with authentication proxyURL := &url.URL{ Scheme: "http", User: url.UserPassword("username", "password"), Host: "proxy.com:8080", } // Create a custom HTTP client with the proxy client := &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyURL(proxyURL), }, } // Use the custom client with the third-party package resp, err := client.PostForm(method, params) if err != nil { // Handle error }
이러한 시나리오에서 대안적인 접근 방식은 필요한 프록시 구성이 필요한 사용자 정의 HTTP 클라이언트를 작성하는 것입니다. 이 클라이언트는 타사 패키지에서 기본 HTTP 클라이언트 대신에 사용할 수 있습니다. HTTP 패키지를 사용하여 프록시 인증을 사용하여 사용자 정의 HTTP 클라이언트를 만드는 방법의 예는 다음과 같습니다. ] 가져 오기 ( "net/http" "net/url" )) // 인증이있는 프록시 URL을 만듭니다 proxyurl : = & url.url { 계획 : "HTTP", 사용자 : url.userpassword ( "username", "password"), 호스트 : "proxy.com:8080", } // 프록시로 사용자 정의 HTTP 클라이언트를 만듭니다 클라이언트 : = & http.client { 운송 : & http.transport { 프록시 : http.proxyurl (proxyurl), }, } // 타사 패키지와 함께 사용자 정의 클라이언트를 사용하십시오 resp, err : = client.Stform (메소드, 매개 변수) err! = nil {인 경우 // 오류를 처리합니다 }
또는 URL은 직접 구문 분석 할 수 있습니다 :
proxyurl, _ : = url.parse ( "http : // username : [email protected] : 8080") 클라이언트 : = & http.client { 운송 : & http.transport { 프록시 : http.proxyurl (proxyurl), }, }
이 메소드는 클라이언트 구성 내 프록시에 필요한 인증 자격 증명을 지정할 수 있습니다.
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3