以后端为目标开始学习 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)
这是什么?\r 重定向到本地语言分支?我是网络技术的新手,所以仍然不知道服务器如何响应以及对用户隐藏但可以与机器一起使用的内容。所以,评论问我file.txt。在这里(PS.解析器效果很好):
google.com; yandex.ru; habr.com; xakep.ru; stackoverflow.com
郎朗坤
相关分类