// request is a common method to send POST request and get []byte response.
request is a common method to send POST request and get []byte response.
// request is a common method to send POST request and get [] byte response .
func request(urlValue string, params *url.Values, timeout time.Duration) ([]byte, error) {
	var resp *http.Response
	req, err := http.NewRequest("POST", urlValue, strings.NewReader(params.Encode()))
	if err != nil {
		return nil, err
	}
	req.Header.Add("User-Agent", userAgent)
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	req = req.WithContext(ctx)
	ec := make(chan error)
	go func() {
		resp, err = httpClient.Do(req)
		ec <- err
		close(ec)
	}()
	select {
	case <-ctx.Done():
		<-ec 
		return nil, fmt.Errorf("timed out (%v)", timeout)
	case err := <-ec:
		if err != nil {
			return nil, err
		}
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("wrong response code=%v", resp.StatusCode)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	return body, nil
}
func request ( urlValue string , params * url . Values , timeout time . Duration ) ( [ ] byte , error ) { var resp * http . Response req , err := http . NewRequest ( " " , urlValue , strings . NewReader ( params . Encode ( ) ) ) if err != nil { return nil , err } req . Header . Add ( " " , userAgent ) req . Header . Set ( " " , " " ) ctx , cancel := context . WithTimeout ( context . Background ( ) , timeout ) defer cancel ( ) req = req . WithContext ( ctx ) ec := make ( chan error ) go func ( ) { resp , err = httpClient . Do ( req ) ec <- err close ( ec ) } ( ) select { case <- ctx . Done ( ) : <- ec return nil , fmt . Errorf ( " " , timeout ) case err := <- ec : if err != nil { return nil , err } } defer resp . Body . Close ( ) if resp . StatusCode != http . StatusOK { return nil , fmt . Errorf ( " " , resp . StatusCode ) } body , err := ioutil . ReadAll ( resp . Body ) if err != nil { return nil , err } return body , nil }
request is a common method to send POST request and get []byte response.
request is a common method to send POST request and get [] byte response .
// wait error "context deadline exceeded"