Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/HTTPS-Authenticated/Go/https-authenticated-2.go
Normal file
49
Task/HTTPS-Authenticated/Go/https-authenticated-2.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
userid = "rosetta"
|
||||
password = "code"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Use custom certificate for testing. Not exactly required by task.
|
||||
b, err := ioutil.ReadFile("cert.pem")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
if ok := pool.AppendCertsFromPEM(b); !ok {
|
||||
log.Fatal("Failed to append cert")
|
||||
}
|
||||
tc := &tls.Config{RootCAs: pool}
|
||||
tr := &http.Transport{TLSClientConfig: tc}
|
||||
client := &http.Client{Transport: tr}
|
||||
req, err := http.NewRequest("GET", "https://127.0.0.1:8080", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// This one line implements the authentication required for the task.
|
||||
req.SetBasicAuth(userid, password)
|
||||
|
||||
// Make request and show output.
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue