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}