sing/common/network/http.go
2022-04-16 17:00:07 +08:00

41 lines
868 B
Go

package network
import (
"context"
"io/ioutil"
"net"
"net/http"
)
func HTTPClientWithNetwork(network string) *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
var dialer net.Dialer
return dialer.DialContext(ctx, network, addr)
},
},
}
}
func Get(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
}
func GetContext(ctx context.Context, client *http.Client, url string) ([]byte, error) {
request, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
}