Invalid character "\r" in host name :51813

时间:2018-09-19 08:23:56

标签: go

Start learning golang with backend aimity. I'm trying to Get.body of webpage at some url. Here's cut of the code.

func AddForm(url string) string {
    if url == "" {
        data := ReadByIoutil("file.txt") // the file contains several urls
        array := ParseData(data) // parsing data in T "Object" => Object.url string
        url = "https://"
        url += array[rand.Intn(len(array))].url // choose randomly 1 among urls
        log.Print(url)
    }
    resp, err := http.Get(url) //get content from url
    check(err)
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    content := string(body)
    WriteByOs("index.html", content) //save to another file (optionaly)
    return content
}

// I practically don't understand this block, but I copied from another place but it works as I wanted
func handler(w http.ResponseWriter, r *http.Request) {
    url := r.FormValue("url")
    if url == "" {
        w.Header().Set("Content-Type", "text/html; charset=utf-8")
        url = ""
        //url = "habr.com"
        fmt.Fprint(w, AddForm(url))
        return
    }
}

I check structure. Everything is fine => Object.url looks like "example.com". Every url in structure is openning well, e.g. google.com, habr.com. Also I get noticed e.g. "[PREFIX].cppreference.com" yell error too. But for me it's clear, but still don't understand how to solve this. THE MAIN problem is I got error and not openning url "stackoverflow.com":

2018/09/19 12:08:16 https://stackoverflow.com
2018/09/19 12:08:16 http: panic serving [::1]:51813: parse https://stackoverflow.com
: invalid character "\r" in host name
goroutine 19 [running]:
net/http.(*conn).serve.func1(0xc00003c8c0)
    C:/Go/src/net/http/server.go:1746 +0xd7
panic(0x665ae0, 0xc0002369f0)
    C:/Go/src/runtime/panic.go:513 +0x1c7
main.check(...)
    C:/Users/dev/go/src/test/main.go:14
main.AddForm(0xc000042800, 0x1b, 0x0, 0x0)
    C:/Users/dev/go/src/test/main.go:100 +0x2e6
main.handler(0x6f1300, 0xc0001447e0, 0xc0000d8c00)
    C:/Users/dev/go/src/test/main.go:114 +0xb2
net/http.HandlerFunc.ServeHTTP(0x6bfb20, 0x6f1300, 0xc0001447e0, 0xc0000d8c00)
    C:/Go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0x899640, 0x6f1300, 0xc0001447e0, 0xc0000d8c00)
    C:/Go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc000048c30, 0x6f1300, 0xc0001447e0, 0xc0000d8c00)
    C:/Go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc00003c8c0, 0x6f1400, 0xc000040280)
    C:/Go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
    C:/Go/src/net/http/server.go:2851 +0x2fc

what is this? \r redirect to local language branch? I'm newbie in Network Tech, so still don't know how servers response and what is hidden from users but works with machines. So, comments ask me file.txt. Here (PS. parser works good):

google.com; yandex.ru; habr.com; xakep.ru; stackoverflow.com

1 个答案:

答案 0 :(得分:1)

删除EOL字符。您可能必须使用“ \ r \ n”或“ \ r”

url += strings.TrimSuffix(array[rand.Intn(len(array))].url, "\r") 
相关问题