mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-24 05:28:49 +02:00
51 lines
915 B
Go
51 lines
915 B
Go
package fetch
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// getPlanHTML Get the HTML document from the specified URL
|
|
|
|
func GetHTML(url string) (string, error) {
|
|
|
|
// Create HTTP client with timeout of 5 seconds
|
|
client := http.Client{
|
|
Timeout: 30 * time.Second,
|
|
}
|
|
|
|
// Send GET request
|
|
response, err := client.Get(url)
|
|
if err != nil {
|
|
fmt.Printf("Error occurred while making the request: %s\n", err.Error())
|
|
return "", err
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
return
|
|
}
|
|
}(response.Body)
|
|
|
|
// Read the response body
|
|
body, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
fmt.Printf("Error occurred while reading the response: %s\n", err.Error())
|
|
return "", err
|
|
}
|
|
|
|
return toUtf8(body), err
|
|
|
|
}
|
|
|
|
func toUtf8(iso88591Buf []byte) string {
|
|
buf := make([]rune, len(iso88591Buf))
|
|
for i, b := range iso88591Buf {
|
|
buf[i] = rune(b)
|
|
}
|
|
return string(buf)
|
|
}
|