Documentation
¶
Index ¶
- type DCHttpClient
- func (v *DCHttpClient) Delete(url string, headers map[string]string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) DoBytes(method string, url string, headers map[string]string, data []byte) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) DoForm(method string, url string, headers map[string]string, form map[string]string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) DoJSON(method string, url string, headers map[string]string, jsonObject interface{}) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) DoMultipartForm(method string, url string, headers map[string]string, form map[string]string, ...) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) DoString(method string, url string, headers map[string]string, text string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) DoWithoutContent(method string, url string, headers map[string]string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) Get(url string, headers map[string]string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) NewCookieJar()
- func (v *DCHttpClient) PostBytes(url string, headers map[string]string, data []byte) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) PostForm(url string, headers map[string]string, form map[string]string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) PostJSON(url string, headers map[string]string, jsonObject interface{}) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) PostMultipartForm(url string, headers map[string]string, form map[string]string, ...) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) PostString(url string, headers map[string]string, text string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) Put(url string, headers map[string]string) (response *DCHttpResponse, err error)
- func (v *DCHttpClient) SetCompression(enable bool)
- func (v *DCHttpClient) SetConnectTimeout(timeout time.Duration)
- func (v *DCHttpClient) SetCookieJar(jar *cookiejar.Jar)
- func (v *DCHttpClient) SetExpectContinueTimeout(timeout time.Duration)
- func (v *DCHttpClient) SetFallbackDelay(delay time.Duration)
- func (v *DCHttpClient) SetKeepAlive(enable bool)
- func (v *DCHttpClient) SetKeepAliveIdleTimeout(timeout time.Duration)
- func (v *DCHttpClient) SetMaxConnsPerHost(size int)
- func (v *DCHttpClient) SetMaxIdleConns(size int)
- func (v *DCHttpClient) SetMaxIdleConnsPerHost(size int)
- func (v *DCHttpClient) SetResponseHeaderTimeout(timeout time.Duration)
- func (v *DCHttpClient) SetTCPKeepAlive(timeout time.Duration)
- func (v *DCHttpClient) SetTLSHandshakeTimeout(timeout time.Duration)
- func (v *DCHttpClient) SetTLSMaxVersion(version uint16)
- func (v *DCHttpClient) SetTLSMinVersion(version uint16)
- func (v *DCHttpClient) SetTLSVerify(enable bool)
- func (v *DCHttpClient) SetTrace(enable bool)
- type DCHttpResponse
- func (v *DCHttpResponse) Close()
- func (v *DCHttpResponse) GetHeader(key string) (value string)
- func (v *DCHttpResponse) GetHeaders() (headers map[string]string)
- func (v *DCHttpResponse) GetProto() (proto string)
- func (v *DCHttpResponse) GetRawResponse() *http.Response
- func (v *DCHttpResponse) GetStatusCode() (statusCode int)
- func (v *DCHttpResponse) GetTraceInfo() (traceInfo *DCHttpTrace)
- func (v *DCHttpResponse) HasHeader(key string) (ok bool)
- func (v *DCHttpResponse) ToBytes() (body []byte, err error)
- func (v *DCHttpResponse) ToString() (body string, err error)
- type DCHttpTrace
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DCHttpClient ¶
type DCHttpClient struct { Dialer *net.Dialer // *net.Dialer Core *http.Client // based http client Transport *http.Transport // client properties Trace bool // enable trace time spent }
Http Client
func NewHttpClient ¶
func NewHttpClient() (client *DCHttpClient)
NewHttpClient help user to create a httpclient
@Return client: DCHttpClient instance
func (*DCHttpClient) Delete ¶
func (v *DCHttpClient) Delete( url string, headers map[string]string) (response *DCHttpResponse, err error)
Send a DELETE request
@Params url: http address headers: add extra headers or which need to be replaced @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) DoBytes ¶
func (v *DCHttpClient) DoBytes( method string, url string, headers map[string]string, data []byte) (response *DCHttpResponse, err error)
Send bytes (without Content-Type) In principle, only Post can attach the body parameter, please make sure your server is compatible with your request method.
@Params method: http method, such as 'http.MethodPost'. url: http address headers: add extra headers or which need to be replaced data: bytes that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) DoForm ¶
func (v *DCHttpClient) DoForm( method string, url string, headers map[string]string, form map[string]string) (response *DCHttpResponse, err error)
Send form (Content-Type: application/x-www-form-urlencoded) In principle, only Post can attach the body parameter, please make sure your server is compatible with your request method.
@Params method: http method, such as 'http.MethodPost'. url: http address headers: add extra headers or which need to be replaced form: form that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) DoJSON ¶
func (v *DCHttpClient) DoJSON( method string, url string, headers map[string]string, jsonObject interface{}) (response *DCHttpResponse, err error)
Send json (Content-Type: application/json) In principle, only Post can attach the body parameter, please make sure your server is compatible with your request method.
@Params method: http method, such as 'http.MethodPost'. url: http address headers: add extra headers or which need to be replaced jsonObject: json object that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) DoMultipartForm ¶
func (v *DCHttpClient) DoMultipartForm( method string, url string, headers map[string]string, form map[string]string, fileParam string, filePath string) (response *DCHttpResponse, err error)
Send multipartform (Content-Type: multipart/form-data) In principle, only Post can attach the body parameter, please make sure your server is compatible with your request method.
@Params method: http method, such as 'http.MethodPost'. url: http address headers: add extra headers or which need to be replaced form: form that needs to be sent fileParam: parameter name of the file. filePath: filePath need to be uploaded @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) DoString ¶
func (v *DCHttpClient) DoString( method string, url string, headers map[string]string, text string) (response *DCHttpResponse, err error)
Send a string (Content-Type: text/plain) In principle, only Post can attach the body parameter, please make sure your server is compatible with your request method.
@Params method: http method, such as 'http.MethodPost'. url: http address headers: add extra headers or which need to be replaced text: the string that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) DoWithoutContent ¶
func (v *DCHttpClient) DoWithoutContent( method string, url string, headers map[string]string) (response *DCHttpResponse, err error)
Send a request without any body
@Params method: http method, such as 'http.MethodPost'. url: http address headers: add extra headers or which need to be replaced @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) Get ¶
func (v *DCHttpClient) Get(url string, headers map[string]string) (response *DCHttpResponse, err error)
Send a get request
@Params url: http address headers: add extra headers or which need to be replaced @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) NewCookieJar ¶ added in v1.0.1
func (v *DCHttpClient) NewCookieJar()
Add a default empty cookies jar
func (*DCHttpClient) PostBytes ¶
func (v *DCHttpClient) PostBytes( url string, headers map[string]string, data []byte) (response *DCHttpResponse, err error)
Send bytes (without Content-Type)
@Params url: http address headers: add extra headers or which need to be replaced data: bytes that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) PostForm ¶
func (v *DCHttpClient) PostForm( url string, headers map[string]string, form map[string]string) (response *DCHttpResponse, err error)
Post a form (Content-Type: application/x-www-form-urlencoded)
@Params url: http address headers: add extra headers or which need to be replaced form: form that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) PostJSON ¶
func (v *DCHttpClient) PostJSON( url string, headers map[string]string, jsonObject interface{}) (response *DCHttpResponse, err error)
Send a json object (Content-Type: application/json)
@Params url: http address headers: add extra headers or which need to be replaced jsonObject: json object that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) PostMultipartForm ¶
func (v *DCHttpClient) PostMultipartForm( url string, headers map[string]string, form map[string]string, fileParam string, filePath string) (response *DCHttpResponse, err error)
Post a multipartform (Content-Type: multipart/form-data) make sure your server is compatible with your request method.
@Params url: http address headers: add extra headers or which need to be replaced form: form that needs to be sent fileParam: parameter name of the file. filePath: filePath need to be uploaded @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) PostString ¶
func (v *DCHttpClient) PostString( url string, headers map[string]string, text string) (response *DCHttpResponse, err error)
Post a string (Content-Type: text/plain)
@Params url: http address headers: add extra headers or which need to be replaced text: the string that needs to be sent @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) Put ¶
func (v *DCHttpClient) Put( url string, headers map[string]string) (response *DCHttpResponse, err error)
Send a PUT request
@Params url: http address headers: add extra headers or which need to be replaced @Return response: http response (*DCHttpResponse) err: error info
func (*DCHttpClient) SetCompression ¶
func (v *DCHttpClient) SetCompression(enable bool)
Whether to disable compression when requested Compression means bringing the "Accept-Encoding: gzip" header to the request.
@Params enable: True means enabled, false means disabled, default is true
func (*DCHttpClient) SetConnectTimeout ¶
func (v *DCHttpClient) SetConnectTimeout(timeout time.Duration)
Timeout of the connection established?
@Params timeout: The default is 30s, 0 means unrestricted. Unrestricted does not mean that there is really no limit, but is limited by the operating system itself. Under normal circumstances, this limit may be around 3 minutes or even shorter.
func (*DCHttpClient) SetCookieJar ¶ added in v1.0.1
func (v *DCHttpClient) SetCookieJar(jar *cookiejar.Jar)
Add a custom cookies jar
@Params jar: the cookies jar, nil means disable cookies
func (*DCHttpClient) SetExpectContinueTimeout ¶
func (v *DCHttpClient) SetExpectContinueTimeout(timeout time.Duration)
For requests with "Expect: 100-continue" header information, wait for the server to grant a timeout for the body to send
@Params timeout: The default is 1s, 0 means no waiting, immediately send the requested body
func (*DCHttpClient) SetFallbackDelay ¶
func (v *DCHttpClient) SetFallbackDelay(delay time.Duration)
With dual-stack support, if the server is also a dual-stack address, the client will first perform a Fallback connection test on the IPv6 address according to the content defined in RFC 6555, and determine whether it needs to be downgraded to the IPv4 request according to the delay of the FallbackDelay.
@Params delay: if set to 0, the system will be set as 300ms, if set to negative, it means disable IPv6
func (*DCHttpClient) SetKeepAlive ¶
func (v *DCHttpClient) SetKeepAlive(enable bool)
Whether to use http keep-alive to reuse connections, including http2
@Params enable: True means enabled, false means off, default is true
func (*DCHttpClient) SetKeepAliveIdleTimeout ¶
func (v *DCHttpClient) SetKeepAliveIdleTimeout(timeout time.Duration)
Set the idle timeout of the keep-alive http layer.
@Params timeout: The default is 90s, 0 means no limit (TCPKeepAlive by default)
func (*DCHttpClient) SetMaxConnsPerHost ¶
func (v *DCHttpClient) SetMaxConnsPerHost(size int)
Set the upper limit of all connections for the connection pool under keep-alive (each domain name is counted separately), MaxConnsPerHost > MaxIdleConnsPerHost
@Params size: The size of the connection pool, 0 means no restrictions on use, the default is 0
func (*DCHttpClient) SetMaxIdleConns ¶
func (v *DCHttpClient) SetMaxIdleConns(size int)
Set the upper limit of the idle connection of the total connection pool under keep-alive
@Params size: The size of the connection pool, 0 means no limit, the default is 0
func (*DCHttpClient) SetMaxIdleConnsPerHost ¶
func (v *DCHttpClient) SetMaxIdleConnsPerHost(size int)
Set the upper limit of the idle connection of the connection pool under keep-alive (each domain name is counted separately), MaxIdleConns has a higher priority than MaxIdleConnsPerHost, that is, IdleConnsPerHost is less than the sum of Hosts <= MaxIdleConns
@Params size: The size of the connection pool, 0 means use tcp.DefaultMaxIdleConnsPerHost, the default is 0
func (*DCHttpClient) SetResponseHeaderTimeout ¶
func (v *DCHttpClient) SetResponseHeaderTimeout(timeout time.Duration)
Timed from the start of the request to send, to the response's head of the service return, timeout setting
@Params timeout: The default is 30s, 0 means no limit
func (*DCHttpClient) SetTCPKeepAlive ¶
func (v *DCHttpClient) SetTCPKeepAlive(timeout time.Duration)
Keep-alive keepalive parameters at the TCP level
@Params timout:The default is 30s. If it is 0, it will be the default value of the operating system. If it is negative, the keep-alive will be disabled.
This parameter corresponds to net.Dailer.KeepAlive, the document explains the mode, here is a supplementary explanation In fact, this value is the tcp_keepalive_intvl and tcp_keepalive_time values at the tcp level. The linux man description is as follows
tcp_keepalive_intvl (integer; default: 75; since Linux 2.4) The number of seconds between TCP keep-alive probes. tcp_keepalive_probes (integer; default: 9; since Linux 2.2) The maximum number of TCP keep-alive probes to send before giving up and killing the connection if no response is obtained from the other end. tcp_keepalive_time (integer; default: 7200; since Linux 2.2) The number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes. Keep-alives are only sent when the SO_KEEPALIVE socket option is enabled. The default value is 7200 seconds (2 hours). An idle connection is terminated after approximately an additional 11 minutes (9 probes an interval of 75 seconds apart) when keep-alive is enabled.
To use the keepalive mechanism, first turn on the SO_KEEPALIVE setting; then the system will initiate the probe after the connection idle 'keepalive_time' time. When consecutive 'keepalive_probes' probes fail, the system closes the connection. 'keepalive_intvl' is the interval between two probes
Refer to the implementation of the go source
setKeepAlive(): syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive)) ... setKeepAlivePeriod(): syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs) syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs)
Golang sets the 'tcp_keepalive_intvl' value and the 'tcp_keepalive_time' value with Dail.KeepAlive. Linux is calculated according to the 9 Probes timeout to close the connection. The lifetime of the connection is the normal request period (continuous idle time < Dail.KeepAlive ) + 'Dail.KeepAlive' + 9 * 'Dail.KeepAlive' time will be closed. Some people will be confused that http.Transport already has control over the idle. Why is this needed? Because this is the parameter exposed by the Net library of golang, and the http library is a separate library, which can be understood as http.Transport has closed the connection in advance according to idleTimeout before the net timeout is closed.
func (*DCHttpClient) SetTLSHandshakeTimeout ¶
func (v *DCHttpClient) SetTLSHandshakeTimeout(timeout time.Duration)
Set the handshake timeout period for TLS
@Params timeout: the default is 10s, 0 means no limit
func (*DCHttpClient) SetTLSMaxVersion ¶
func (v *DCHttpClient) SetTLSMaxVersion(version uint16)
Set the maximum protocol version number of TLS
@Params version: 0 means <= TLS 1.3, other values can read tls library, for example tls.VersionTLS12, default 0
func (*DCHttpClient) SetTLSMinVersion ¶
func (v *DCHttpClient) SetTLSMinVersion(version uint16)
Set the minimum protocol version number of TLS
@Params version: 0 means >= TLS 1.0, other values can read tls library, for example tls.VersionTLS12, default 0
func (*DCHttpClient) SetTLSVerify ¶
func (v *DCHttpClient) SetTLSVerify(enable bool)
Is it necessary to verify the validity of the certificate for TLS?
@Params enable: True means check, false means no check, default is true
func (*DCHttpClient) SetTrace ¶
func (v *DCHttpClient) SetTrace(enable bool)
Whether you need to perform performance statistics on http requests, such as dns time-consuming, etc.
@Params enable: true means traceMode on
type DCHttpResponse ¶
type DCHttpResponse struct { Raw *http.Response // *http.Response Header map[string]string // Cache *http.Response.Header to map[string]string TraceInfo *DCHttpTrace // Step-by-step timing information for an http request // contains filtered or unexported fields }
DCHttpResponse: Http response from DCHttpClient
func (*DCHttpResponse) Close ¶
func (v *DCHttpResponse) Close()
When getting a Response, if the body information has never been read, then the input stream of the body is open and needs to be closed once.
func (*DCHttpResponse) GetHeader ¶
func (v *DCHttpResponse) GetHeader(key string) (value string)
Returns the header information @Param key: header key @Return value: the value of header key
func (*DCHttpResponse) GetHeaders ¶
func (v *DCHttpResponse) GetHeaders() (headers map[string]string)
Get all header information @Return headers: all header information
func (*DCHttpResponse) GetProto ¶
func (v *DCHttpResponse) GetProto() (proto string)
Get http protocol information, such as "HTTP/1.0"
func (*DCHttpResponse) GetRawResponse ¶
func (v *DCHttpResponse) GetRawResponse() *http.Response
GetRawResponse return golang native response
func (*DCHttpResponse) GetStatusCode ¶
func (v *DCHttpResponse) GetStatusCode() (statusCode int)
Get the http status code
func (*DCHttpResponse) GetTraceInfo ¶
func (v *DCHttpResponse) GetTraceInfo() (traceInfo *DCHttpTrace)
Get the step-by-step time-consuming information for this HTTP request
@premise: DCHttpClient.SetTrace(true) @Return traceInfo: the time-consuming information result
func (*DCHttpResponse) HasHeader ¶
func (v *DCHttpResponse) HasHeader(key string) (ok bool)
Determine whether the specified header information is included @Param key: header key @Return ok: true means 'included'
func (*DCHttpResponse) ToBytes ¶
func (v *DCHttpResponse) ToBytes() (body []byte, err error)
ToBytes returned body([]bytes)
func (*DCHttpResponse) ToString ¶
func (v *DCHttpResponse) ToString() (body string, err error)
ToString convert body([]bytes) to string
type DCHttpTrace ¶
type DCHttpTrace struct { TotalSart time.Time // Request start time TotalEnd time.Time // Request completion time TotalDuration time.Duration // Total time spent on one request. DNSStart time.Time // Start lookup DNS. If the request is sent through the proxy, maybe value is zero DNSDone time.Time // Finished lookup DNS. If the request is sent through the proxy, maybe value is zero DNSDuration time.Duration // If the request is sent through the proxy, maybe value is zero ConnectStart time.Time // TCP connect start ConnectDone time.Time // TCP connected or failed ConnectDuration time.Duration // Time spent on TCP connecting TLSHandshakeStart time.Time // Start TLS handshake TLSHandshakeDone time.Time // Finished TLS handshake (Success or Failed) TLSHandshakeDuration time.Duration // Time spent on TLS handshake RequestSended time.Time // Finished tp send request (headers + body) ResponseReturned time.Time // Got the first bytes of response ServerDuration time.Duration // The time taken by the server to process this request (including the time when the Response is returned on the road) }
DCHttpTrace: Step-by-step timing information for an http request