fix
liujiandao
2024-05-06 5f4ff02960658a1ffca65a8d1eb5425faca6ecee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package http
 
import (
    "errors"
    "fmt"
    "io"
    "net/http"
    "time"
)
 
// 调用此函数需要手动close,因为本函数中并未close
func HttpGetWithReadCloser(url string) (io.ReadCloser, error) {
    request, err := http.NewRequest("GET", url, nil)
    if err != nil {
        return nil, err
    }
 
    client := new(http.Client)
    client.Timeout = time.Second * 600
 
    resp, err := client.Do(request)
    if err != nil {
        return nil, err
    }
 
    if resp.StatusCode != 200 {
        return nil, errors.New(fmt.Sprintf("请求失败, 状态码:%d", resp.StatusCode))
    }
 
    if resp.Body == nil {
        return nil, errors.New("body is null")
    }
 
    return resp.Body, nil
}