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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配