0

I am trying to make a GET request against an HTTPS URL using proxy with username/password authorization (auth is required by the proxy not the website). Here's what I do:

package main

import (
    "crypto/tls"
    "encoding/base64"
    "fmt"
    "net/http"
    "net/http/httputil"
    "net/url"
)

func main() {
    ua := "Mozilla/5.0 (Windows NT 6.1"
    basic := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:mypass"))
    req, err := http.NewRequest("GET", "https://api.ipify.org/", nil)
    proxyUrl, _ := url.Parse("http://myproxy.com:9999")
    fmt.Println(basic) // Basic dXNlcm5hbWU6bXlwYXNz
    req.Header.Add("Proxy-Authorization", basic)
    req.Header.Add("User-Agent", ua)
    bb, _ := httputil.DumpRequest(req, false)
    fmt.Println(string(bb))
    /*
    Get / HTTP/1.1
    Host: api.ipify.org
    Proxy-Authorization: Basic dXNlcm5hbWU6bXlwYXNz
    User-Agent: Mozilla/5.0 (Windows NT 6.1
    */
    client := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
            Proxy:           http.ProxyURL(proxyUrl),
        },
    }
    resp, err := client.Do(req)
    fmt.Println(err)  // Proxy Authentication Required
    fmt.Println(resp) // <nil>
}

The catch is that when I try to do a request to an HTTP (not HTTPS) site it goes fine, but if I make HTTPS request it fails (see above the message).

I tested the proxy with my browser (FireFox) and everything goes well, I looked for the headers through firebug and added everything relevant to the request (above). I've double-triple-checked the basic value and everything else but without any luck.

So does some one have any idea why this happens or at least how do I research the problem?

The last thing to add is that I can use a public HTTP proxy (that doesn't require any auth) in this case and problems seem to start when auth enters in this process (the error also suggests that).

P.S. Unfortunately I cannot share the proxy IP, port and username cause it is against their policy.

1 Answer 1

3
package main

import (
    "crypto/tls"
    "fmt"
    "net/url"
    "net/http"
)

func main() {
    req, err := http.NewRequest("GET", "https://api.ipify.org/", nil)
    proxyUrl, _ := url.Parse("http://username:[email protected]:9999")
    client := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
            Proxy:           http.ProxyURL(proxyUrl),
        },
    }
    _, err = client.Do(req)
    fmt.Println(err)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I actually came across something like that but could not believe it.
@Gonzalez please, check update solution. In my tests this works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.