diff --git a/linkchecker.go b/linkchecker.go index 7a7c6da..dfc06d9 100644 --- a/linkchecker.go +++ b/linkchecker.go @@ -1,5 +1,7 @@ package main +const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36" + import ( "fmt" "net/http" @@ -120,7 +122,13 @@ func (lc *LinkChecker) checkLinksRecursive(pageURL string, brokenLinks []BrokenL } func (lc *LinkChecker) getLinks(pageURL string) ([]string, error) { - resp, err := lc.client.Get(pageURL) + req, err := http.NewRequest("GET", pageURL, nil) + if err != nil { + return nil, err + } + req.Header.Set("User-Agent", userAgent) + + resp, err := lc.client.Do(req) if err != nil { return nil, err } @@ -169,13 +177,19 @@ func (lc *LinkChecker) getLinks(pageURL string) ([]string, error) { } func (lc *LinkChecker) isLinkValid(link string) (int, *RedirectInfo, error) { + req, err := http.NewRequest("GET", link, nil) + if err != nil { + return 0, nil, err + } + req.Header.Set("User-Agent", userAgent) + client := &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, } - resp, err := client.Get(link) + resp, err := client.Do(req) if err != nil { return 0, nil, err }