package main


import (

    "fmt"

    "io/ioutil"

    "log"

    "net/http"

)


func main() {


    client := &http.Client{

         Timeout: 1 * time.Second,

    }

    request, err := http.NewRequest("GET", "https://ifconfig.co", nil)


    if err != nil {

        log.Fatal(err)

    }


    response, err := client.Do(request)

    defer response.Body.Close()

    body, err := ioutil.ReadAll(response.Body)


    if err != nil {

        log.Fatal(err)

    }


    fmt.Printf("%s", body)

}


package main




import (

    "fmt"

    "io/ioutil"

    "log"

    "net/http"

    "net/http/httputil"

    "os"

)




func main() {


     debug := os.Getenv("DEBUG")

     client := &http.Client{}

     request, err := http.NewRequest("GET", "https://ifconfig.co", nil)


     if err != nil {

         log.Fatal(err)

     }


     if debug == "1" {

         debugRequest, err := httputil.DumpRequestOut(request, true)


if err != nil {

             log.Fatal(err)

         }


         fmt.Printf("%s", debugRequest)

    }


    response, err := client.Do(request)

    defer response.Body.Close()




    if debug == "1" {

        debugResponse, err := httputil.DumpResponse(response, true)


        if err != nil {

            log.Fatal(err)

        }


        fmt.Printf("%s", debugResponse)

    }


    body, err := ioutil.ReadAll(response.Body)


    if err != nil {

        log.Fatal(err)

    }


    fmt.Printf("%s\n", body)

}



tr := &http.Transport{

    DialContext: (&net.Dialer{

        Timeout:   30 * time.Second,

        KeepAlive: 30 * time.Second,

    }).DialContext,


    TLSHandshakeTimeout:   10 * time.Second,

    IdleConnTimeout:       90 * time.Second,

    ResponseHeaderTimeout: 10 * time.Second,

    ExpectContinueTimeout: 1 * time.Second,

}


client := &http.Client{

    Transport: tr,

}

'Language > go' 카테고리의 다른 글

regexp 코드  (0) 2018.03.15
json 코드  (0) 2018.03.15
http 서버 코드  (0) 2018.03.15
goroutine 기본 구조  (0) 2018.01.02
go error handling  (0) 2018.01.02

+ Recent posts