* func SetTimeOut(timeDuration time.Duration) { if timeDuration != 0 { defaultTimeout = timeDuration } } */
(reqUrl string, reqParams map[string]string, headers map[string]string)
| 164 | } */ |
| 165 | |
| 166 | func (target HttpClient) get(reqUrl string, reqParams map[string]string, headers map[string]string) (string, error) { |
| 167 | urlParams := url.Values{} |
| 168 | Url, _ := url.Parse(reqUrl) |
| 169 | for key, val := range reqParams { |
| 170 | urlParams.Set(key, val) |
| 171 | } |
| 172 | |
| 173 | // client |
| 174 | httpClient := getClient(reqUrl, target.TimeOut) |
| 175 | |
| 176 | //如果参数中有中文参数,这个方法会进行URLEncode |
| 177 | Url.RawQuery = urlParams.Encode() |
| 178 | // 得到完整的url,http://xx?query |
| 179 | urlPath := Url.String() |
| 180 | |
| 181 | httpRequest, err := http.NewRequest("GET", urlPath, nil) |
| 182 | if err != nil { |
| 183 | return "", err |
| 184 | } |
| 185 | // 添加请求头 |
| 186 | for k, v := range headers { |
| 187 | httpRequest.Header.Add(k, v) |
| 188 | } |
| 189 | |
| 190 | // 发送请求 |
| 191 | SmartIDELog.Debug(formatRequest(httpRequest, nil)) |
| 192 | httpResponse, err := httpClient.Do(httpRequest) |
| 193 | if err != nil { |
| 194 | return "", err |
| 195 | } |
| 196 | responseBody, err := parsingResponse(httpResponse) |
| 197 | if err != nil { |
| 198 | return "", err |
| 199 | } |
| 200 | defer httpResponse.Body.Close() |
| 201 | if httpResponse.StatusCode != 200 { |
| 202 | return "", errors.New(httpResponse.Status) |
| 203 | } |
| 204 | |
| 205 | if len(string(responseBody)) == 0 { |
| 206 | return "", errors.New("reponse body is empty") |
| 207 | } |
| 208 | return string(responseBody), nil |
| 209 | } |
| 210 | |
| 211 | func (target HttpClient) put(reqUrl string, reqParams map[string]interface{}, headers map[string]string) (string, error) { |
| 212 | jsonBytes, err := json.Marshal(reqParams) |
no test coverage detected