dongweihuai5601 2016-11-26 11:16
浏览 409
已采纳

通过具有基本身份验证的HTTP代理访问HTTPS

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条回答 默认 最新

  • duanhan8757 2016-11-27 19:02
    关注
    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:password@127.0.0.1:9999")
        client := &http.Client{
            Transport: &http.Transport{
                TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
                Proxy:           http.ProxyURL(proxyUrl),
            },
        }
        _, err = client.Do(req)
        fmt.Println(err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度