go - 主机名 :51813 中的无效字符 "\r"

标签 go

以后端为目标开始学习 golang。我正在尝试在某个网址获取网页的正文。这是代码的一部分。

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
    }
}

我检查结构。一切都很好 => Object.url 看起来像“example.com”。结构中的每个 url 都打开得很好,例如google.com、habr.com。我也注意到了,例如“[PREFIX].cppreference.com”也大喊错误。但对我来说很清楚,但仍然不明白如何解决这个问题。主要问题是我遇到错误并且没有打开 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

这是什么?\r 重定向到本地语言分支?我是网络技术的新手,所以仍然不知道服务器如何响应以及对用户隐藏但可以与机器一起使用的内容。 所以,评论问我file.txt。这里(PS.parser 工作得很好):

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

最佳答案

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

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

关于go - 主机名 :51813 中的无效字符 "\r",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52401365/

相关文章:

go - 使用 httptest 为多个处理程序提供服务以模拟多个请求

templates - 在Go模板中按名称访问结构成员

go - "go test"和 "bazel test"中的不同文件权限错误

go - 开发多模块 Go 工作区的问题

mongodb - 如何通过MongoDB同步在两个不同服务器上运行的两个应用

templates - 如何使用结构或变量值的字段作为模板名称?

http - 如何倒带 io.ReadCloser

go - 如何获得最小的非零float64?

go - 遍历树并使用可重用组件提取信息

立即调用 Shutdown 时 http.Server Serve 方法挂起