response_windows.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //go:build windows
  2. package yu_curl
  3. import (
  4. "errors"
  5. yu_http "gogs.qqck.cn/s/tools/http"
  6. )
  7. func (t *Request) resp_reset() {
  8. t.resp_body = nil
  9. t.resp_headers_raw = nil
  10. t.resp_headers = make(map[string]string)
  11. t.resp_cookies = make(map[string]string)
  12. }
  13. // RespErrCode
  14. //
  15. // @Description: 获取响应状态,E_OK 为成功,其他错误参考 E_
  16. func (t *Request) RespErrCode() uintptr {
  17. return t.perform
  18. }
  19. // RespErr
  20. //
  21. // @Description: 获取响应错误信息,nil 为成功
  22. func (t *Request) RespErr() error {
  23. if t.perform == E_OK {
  24. return nil
  25. }
  26. return errors.New(curl_easy_strerror.CallGbkToUtf8(t.perform))
  27. }
  28. // -----------------------------------------------------------------------------------------------------------------------------------------------Content-Type
  29. // RespContentType
  30. //
  31. // @Description: 响应"Content-Type"类型
  32. func (t *Request) RespContentType() (v string) {
  33. return t.resp_headers["Content-Type"]
  34. }
  35. // -----------------------------------------------------------------------------------------------------------------------------------------------Cookies
  36. // RespCookie
  37. //
  38. // @Description: 获取单个响应 Cookie
  39. func (t *Request) RespCookie(name string) string {
  40. return t.resp_cookies[name]
  41. }
  42. // RespCookies
  43. //
  44. // @Description: 获取全部响应 Cookie
  45. func (t *Request) RespCookies() string {
  46. if len(t.resp_cookies) == 0 {
  47. return ""
  48. }
  49. j_cookies := make([]byte, 0, 1024)
  50. for j_name, j_value := range t.resp_cookies {
  51. j_cookies = append(j_cookies, j_name...)
  52. j_cookies = append(j_cookies, '=')
  53. j_cookies = append(j_cookies, j_value...)
  54. j_cookies = append(j_cookies, ';')
  55. j_cookies = append(j_cookies, ' ')
  56. }
  57. return string(j_cookies)
  58. }
  59. // -----------------------------------------------------------------------------------------------------------------------------------------------Headers
  60. // RespHeader
  61. //
  62. // @Description: 获取单个响应 Header
  63. func (t *Request) RespHeader(name string) string {
  64. name = yu_http.Normalize(name)
  65. if name == "Cookie" {
  66. return t.RespCookies()
  67. } else {
  68. return t.resp_headers[name]
  69. }
  70. }
  71. // RespHeaders
  72. //
  73. // @Description: 获取全部响应 Headers
  74. func (t *Request) RespHeaders() string {
  75. if len(t.resp_headers) == 0 {
  76. return ""
  77. }
  78. j_headers := make([]byte, 0, 4096)
  79. for j_name, j_value := range t.resp_headers {
  80. j_headers = append(j_headers, j_name...)
  81. j_headers = append(j_headers, ':')
  82. j_headers = append(j_headers, ' ')
  83. j_headers = append(j_headers, j_value...)
  84. j_headers = append(j_headers, 13, 10)
  85. }
  86. return string(j_headers)
  87. }
  88. // RespHeadersRaw
  89. //
  90. // @Description: 获取全部响应原始 Headers 数据
  91. func (t *Request) RespHeadersRaw() string {
  92. return string(t.resp_headers_raw)
  93. }
  94. // -----------------------------------------------------------------------------------------------------------------------------------------------Utils
  95. // RespStatusCode
  96. //
  97. // @Description: 获取响应 HTTP 状态码
  98. func (t *Request) RespStatusCode() int {
  99. j_result := 0
  100. t.perform, _, _ = curl_easy_getinfo.Call(t.curl, INFO_RESPONSE_CODE, &j_result)
  101. return j_result
  102. }
  103. // RespLen
  104. //
  105. // @Description: 获取响应数据尺寸
  106. func (t *Request) RespLen() int {
  107. return len(t.resp_body)
  108. }
  109. // RespBody
  110. //
  111. // @Description: 获取响应数据
  112. func (t *Request) RespBody() []byte {
  113. return t.resp_body
  114. }
  115. // RespBodyS
  116. //
  117. // @Description: 获取响应数据
  118. func (t *Request) RespBodyS() string {
  119. return string(t.resp_body)
  120. }