Created
May 24, 2023 13:51
-
-
Save HalCanary/251f328c358e0bd45a16f008b949954a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"net/http" | |
"net/url" | |
"strings" | |
) | |
func responseHandler(resp *http.Response) (io.ReadCloser, error) { | |
if resp.StatusCode > 399 { | |
resp.Body.Close() | |
return nil, fmt.Errorf("error: %q %s", resp.Request.URL.String(), resp.Status) | |
} | |
return resp.Body, nil | |
} | |
func doPost(client *http.Client, data url.Values, loc, ref, userAgent string) (io.ReadCloser, error) { | |
req, err := http.NewRequest("POST", loc, strings.NewReader(data.Encode())) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
req.Header.Add("User-Agent", userAgent) | |
if ref != "" { | |
req.Header.Add("Referer", ref) | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
return responseHandler(resp) | |
} | |
func doGet(client *http.Client, loc, ref, userAgent string) (io.ReadCloser, error) { | |
req, err := http.NewRequest("GET", loc, nil) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("User-Agent", userAgent) | |
if ref != "" { | |
req.Header.Add("Referer", ref) | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
return responseHandler(resp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment