Golang中net::ERR_CONTENT_LENGTH_MISMATCH错误的解决方法
Golang中net::ERR_CONTENT_LENGTH_MISMATCH错误的解决方法 我在使用一个Go应用程序提供Angular应用服务时遇到了问题,当访问HTTP服务器时,浏览器控制台返回错误:net::ERR_CONTENT_LENGTH_MISMATCH
Angular应用程序存储在名为dist的文件夹中。
以下是提供应用程序服务的代码:
//create new gorilla mux router
r := mux.NewRouter()
// Handle all preflight request
r.Methods("OPTIONS").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// fmt.Printf("OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, Access-Control-Request-Headers, Access-Control-Request-Method, Connection, Host, Origin, User-Agent, Referer, Cache-Control, X-header")
w.WriteHeader(http.StatusNoContent)
return
})
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
r.Handle("/socket.io/", server)
r.Handle("/login", http.StripPrefix("/login", http.FileServer(http.Dir(dir+"/client/"))))
r.Handle("/dashboard", http.StripPrefix("/dashboard", http.FileServer(http.Dir(dir+"/client/"))))!
r.PathPrefix("/").Handler(http.FileServer(http.Dir(dir + "/client/")))
srv := &http.Server{
Handler: r,
Addr: "0.0.0.0:2000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
以下是我收到的错误信息:

更多关于Golang中net::ERR_CONTENT_LENGTH_MISMATCH错误的解决方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html
恐怕这听起来像是您的环境出了问题,而不是Go服务器端的问题。
更多关于Golang中net::ERR_CONTENT_LENGTH_MISMATCH错误的解决方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
这些请求也需要一分钟才能完成(最终出错)。我会检查 Windows 上常见的可疑因素——杀毒软件,如果使用了代理的话可能还要检查代理设置。
我能够在本地无问题地访问。问题似乎出现在通过VPN连接访问Web服务器时。这看起来是问题的根源?除非还存在其他问题。
出现错误的请求似乎是随机的。有时只有一个请求会出错(失败),有时会有多个请求出错。
这个错误通常发生在HTTP响应头中的Content-Length值与实际传输的数据长度不匹配时。在Go的HTTP文件服务器中,这可能是由于文件在传输过程中被修改,或者文件系统权限问题导致的。
以下是几种可能的解决方案:
1. 使用http.ServeContent替代FileServer
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filePath := dir + "/client/" + r.URL.Path
http.ServeFile(w, r, filePath)
})
2. 禁用Content-Length头(不推荐用于生产环境)
r.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Del("Content-Length")
http.FileServer(http.Dir(dir+"/client/")).ServeHTTP(w, r)
}))
3. 检查并修复文件路径处理
你的代码中路径处理可能有问题,特别是使用了http.StripPrefix。尝试简化文件服务:
// 创建文件服务器
fs := http.FileServer(http.Dir(dir + "/client"))
// 更清晰的路径处理
r.Handle("/login", fs)
r.Handle("/dashboard", fs)
r.PathPrefix("/").Handler(fs)
4. 完整的修复版本
package main
import (
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
// CORS处理
r.Methods("OPTIONS").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
w.WriteHeader(http.StatusNoContent)
})
// 获取当前目录
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
clientDir := dir + "/client"
// 创建文件服务器
fs := http.FileServer(http.Dir(clientDir))
// 路由设置
r.Handle("/socket.io/", server) // 假设server已定义
r.Handle("/login", fs)
r.Handle("/dashboard", fs)
r.PathPrefix("/").Handler(fs)
srv := &http.Server{
Handler: r,
Addr: "0.0.0.0:2000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
5. 添加错误处理中间件
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}
// 使用中间件
r.Use(loggingMiddleware)
主要问题可能出现在路径处理和http.StripPrefix的使用上。确保Angular应用的静态文件正确放置在client目录中,并且服务器有权限读取这些文件。

