LoginSignup
4
4

More than 5 years have passed since last update.

Goのnet/httpで、認証Proxyを超えてみる

Last updated at Posted at 2016-05-02

一発で超えれるような便利関数は見当たらなかったので調べながら試したので備忘録。

package main

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

var (
    user string
    password string
    proxyUrl string    
)

// IDとPW
auth := user + ":" + password

// base64に変換
basic := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))

// Transportを用意
tr := http.Transport{}
parseProxyUrl, _ := url.Parse(proxyUrl)
tr.Proxy = http.ProxyURL(parseProxyUrl)

// httpClientを作成
client := &http.Client{Transport:&tr}

// リクエストを作成
req,_ := http.NewRequest("GET", url, nil)

// ヘッダーにProxy-Authorizationを追加
req.Header.Add("Proxy-Authorization", basic)

// アクセス
resp,err := client.Do(req)

色々省略しているがこんな感じで。
でも、これだとHTTPSのURLにアクセスするとStatusCodeが000になる。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4