【发布时间】:2013-01-05 00:33:45
【问题描述】:
我认为我最初的问题(见栏下方)过于含糊。我编了以下傻 举例说明我的观点。
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
http.HandleFunc("/redir", redirHandler)
http.HandleFunc("/", rootHandler)
_ = http.ListenAndServe("localhost:4000", nil)
}
func redirHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello, this is redir which will host the foreign html page")
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello, this is root")
// at some point "foreign webserver" sends me a complete
// html page.
// I want to serve this page at "/redir/", and I feel my
// best shot is to redirect to "/redir"
time.Sleep(2 * time.Second)
http.Redirect(w, r, "/redir/", http.StatusFound)
}
此代码不起作用,因为我无法重复使用 w 来执行重定向。 那么,在这种情况下是否可以重定向,例如通过另一个 http包调用?
我希望我已经说清楚了......
提前致谢
克里斯
我正在以下配置中的“我的服务器”上工作
[web browser client] -- [my server] -- [foreign webservice]
“我的服务器”向“网络浏览器客户端”提供以下内容
func main() {
http.HandleFunc("/launch", launchHandler)
http.HandleFunc("/", rootHandler)
http.Handle("/socket", websocket.Handler(socketHandler))
err := http.ListenAndServe(listenAddr, nil)
if err != nil {
log.Fatal(err)
}
}
RootHandler 将 html 页面写入“Web 浏览器客户端”,而 socketHandler 在“网络浏览器客户端”和 “我的服务器”。
来自“网络浏览器客户端”的请求通过网络套接字连接到达,并且 由“我的服务器”处理,“我的服务器”与“外部网络服务”通信 来确定响应。
通常,“外国网络服务”会提供一些数据,供以下用户使用 “我的服务器”响应“网络浏览器客户端”。
在某些情况下,“外部网络服务”会以完整的 html 页面进行响应。
我不确定如何将此类 html 页面传达给“网络浏览器客户端”
到目前为止,我最好的猜测是执行 http.Redirect() 到 launchHandler 这样 即launchHandler可以编写从“外国Web服务器”获得的html页面 到“网络浏览器客户端”。 (web socket连接关闭时没有问题。)
为此,我使用 http.ResponseWriter, *http.Request 作为参数 到首先在 rootHandler 中接收到的 httpRedirect。
这不起作用并产生日志消息:“http:multiple response.WriteHeader calls”
关于如何解决此问题的任何建议?也许我执行 http.Redirect 的方法 是错的;也许我应该获得另一个 http.ResponseWriter 和/或 *http.Request 价值观。
感谢任何帮助!
【问题讨论】:
-
鉴于到目前为止没有响应,您可能需要提供导致此问题的实际代码。我承认,根据您的描述,我对您想要实现的目标有些困惑。
标签: go