duanlachu7344 2016-07-19 17:17
浏览 135
已采纳

使用Go和Localhost拒绝连接来制作简单的FileServer

Now I have the following code extracted from a book.

package main

import (
    "net/http"
)

func main() {
    h := http.FileServer(http.Dir("."))
    http.ListenAndServeTLS(":8001", "rui.crt", "rui.key", h)
}

I expect it can list all the file in the folder of main.go but when I browse to:
https://localhost:8001
I can only see:

This site can’t be reached.  
localhost refused to connect.

I use LiteIDE to build and run the program. After <kbd>BuildAndRun</kbd> clicked, the following messages are shown.

F:/Go/bin/go.exe build -i [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.
E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server/server.exe  [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.

Why and how can I fix it?

  • 写回答

1条回答 默认 最新

  • dqybeh2884 2016-07-19 19:35
    关注

    your system cannot find the certificate file.:
    this error means you need "rui.crt" file alongside with your main binary file.
    if you do not have certificate see: How to create a self-signed certificate with openssl?

    then copy "server.pem", "server.key" files to your binary(.exe) file directory

    and run this sample code (for test):

    package main
    
    import (
        "fmt"
        "log"
        "net/http"
    )
    
    type server struct {
    }
    
    func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "*Hello World
    *")
    }
    
    func main() {
        if err := http.ListenAndServeTLS(":443", "server.pem", "server.key", server{}); err != nil {
            log.Fatal(err)
        }
    }
    

    then open web page: https://127.0.0.1/
    if firewall poped up say yes,
    if you see There is a problem with this website’s security certificate. say continue (advance).

    output:

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

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起