Golang HTTP Timeout Setting method

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
c := http.Client{    Transport: &http.Transport{        Dial: func(netw, addr string) (net.Conn, error) {        deadline := time.Now().Add(25 * time.Second)        c, err := net.DialTimeout(netw, addr, time.Second*20)        if err != nil {            return nil, err        }        c.SetDeadline(deadline)        return c, nil        },    },    }c.Get("http://www.qq.com")

//////////////////////////////////////////////

Upload request func Netuploadjson (addr string, buf interface{}) (*[]byte, *int, error) {//will need to upload json to Byte V, _: = json. Marshal (BUF)//upload JSON data req, e: = http. Newrequest ("POST", addr, bytes. Newreader (v)) if E! = nil {///commits an exception, returns an error return nil, nil, E}//Body Type req. Header.set ("Content-type", "Application/json")//Disconnect req after completion. Header.set ("Connection", "close")//-------------------------------------------//Set TimeOut defaultclient: = h Ttp. client{Transport: &http. transport{Dial:func (netw, addr string) (net. Conn, error) {deadline: = time. Now (). ADD (* time. Second) C, err: = Net. Dialtimeout (NETW, addr, time.        SECOND*30) If err! = Nil {return nil, err} c.setdeadline (deadline) return C, nil  },},}//-------------------------------------------//Execute RESP, ee: = defaultclient.do (req) if EE! = Nil {//commit exception, return error returned nil, nil, EE}//Guaranteed I/o normal shutdown defer resp. Body.close ()//Determine the return status if Resp. StatusCode = = http. Statusok {//Read data returned, err: = Ioutil. ReadAll (resp. Body) If err! = Nil {//Read exception, returns error return nil, nil, err}//returns the received data with status return &data, & Resp. StatusCode, nil} else if resp. StatusCode! = http. Statusok {//Returns the exception status return nil, &resp. StatusCode, nil}//will not be here return nil, nil, nil}//download file func netdownloadfile (addr string) (*[]byte, *int, *http. Header, error) {//upload JSON data req, e: = http. Newrequest ("GET", addr, nil) if E! = Nil {//returns exception return nil, nil, nil, E}//Disconnect after completion req. Header.set ("Connection", "close")//-------------------------------------------//Set TimeOut defaultclient: = h Ttp. client{Transport: &http. transport{Dial:func (netw, addr string) (net. Conn, error) {deadline: = time. Now (). ADD (* time. Second) C, err: = Net. Dialtimeout (NETW, addr, time. SECOND*30) If Err! = Nil {return nil, err} c.setdeadline (deadline) return C, nil},},}/    /-------------------------------------------//Execute RESP, ee: = defaultclient.do (req) if ee! = Nil {//return exception Return nil, nil, nil, EE}//ensure normal I/O shutdown defer resp. Body.close ()//Determine the request status if Resp. StatusCode = = $ {data, err: = Ioutil. ReadAll (resp. Body) If err! = Nil {//Read error, returns exception return nil, nil, nil, err}//success, return data and Status return &data, & Amp;resp. StatusCode, &resp. Header, nil} else {//failed, return status returns Nil, &resp. StatusCode, nil, nil}//will not be here return nil, nil, nil, nil}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.