dscpg80066 2018-11-08 19:28
浏览 112
已采纳

如何在处理函数func中使用http.Get请求

How to make http.Get request inside of handler func? For example, this simple code "should" return blank page in localhost:8080 browser but it go nuts. What I have missed in school?

package main

import "net/http"

func index(w http.ResponseWriter, r *http.Request) {
    _, err := http.Get("www.google.com")
    if err != nil {
        panic(err)
    }
}

func main() {
    http.HandleFunc("/", index)
    http.ListenAndServe(":8080", nil)
}
  • 写回答

1条回答 默认 最新

  • douzhaiyuan1731 2018-11-08 20:18
    关注

    The problem is that you should use a protocol (e.g. https://) in the Get function:

    _, err := http.Get("https://www.google.com")
    

    The error in you original code is Get www.google.com: unsupported protocol scheme "".

    enter image description here

    http.Get() expects a URL, and www.google.com is not a URL; a URL begins with a scheme. Even though it is common to type "www.google.com" into a browser, it's still not a full URL; friendly browsers automatically prepend "http://" or "https://" before issuing the request. http.Get() isn't going to do that; it expects a well-formed URL to begin with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度