K
K
Koteezy2018-08-16 18:52:23
go
Koteezy, 2018-08-16 18:52:23

How to correctly set a proxy like user:[email protected]:port?

How to correctly set a proxy like user:[email protected]:port for http.Client ( Transport ? )
Now I'm trying to set it like this:

usr := url.UserPassword("log", "pass")
u := url.URL{
    User: usr,
    Host: "host",
}
tr.Proxy = http.ProxyURL(&u)

But I get an error when requesting: Authentication Required
When I try to set via url.Parse (..), I get this:
{
"Scheme":"Тутлогин",
"Opaque":"пароль@ип:порт",
"User":null,
"Host":"",
"Path":"",
"RawPath":"",
"ForceQuery":false,
"RawQuery":"",
"Fragment":""
}

And this is not at all what you need ..
upd:
ok. I realized that if you feed Parse exactly the url (as it is, and it is written in the method description), then the output is already a normal version of http.Url, but still without a login and password.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
igorzakhar, 2018-08-16
@igorzakhar

Omitted error handling.

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

func main() {

  urlStr := "http://..."

  request, err := http.NewRequest("GET", urlStr, nil)
  proxyURL, err := url.Parse("http://username:[email protected]:port")

  transport := &http.Transport{
    Proxy: http.ProxyURL(proxyURL),
  }

  client := &http.Client{
    Transport: transport,
  }

  response, err := client.Do(request)

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question