栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

转到http,使用客户端将传入的http.request发送到其他服务器。

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

转到http,使用客户端将传入的http.request发送到其他服务器。

您需要将所需的值复制到新请求中。由于这与反向代理的工作非常相似,因此您可能需要查看“ net / http /
httputil”的
作用

ReverseProxy

创建一个新请求,然后仅将要发送的请求部分复制到下一台服务器。如果您打算在两个地方都使用它,则还需要读取和缓冲请求正文:

func handler(w http.ResponseWriter, req *http.Request) {    // we need to buffer the body if we want to read it here and send it    // in the request.     body, err := ioutil.ReadAll(req.Body)    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    // you can reassign the body if you need to parse it as multipart    req.Body = ioutil.NopCloser(bytes.NewReader(body))    // create a new url from the raw RequestURI sent by the client    url := fmt.Sprintf("%s://%s%s", proxyScheme, proxyHost, req.RequestURI)    proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body))    // We may want to filter some headers, otherwise we could just use a shallow copy    // proxyReq.Header = req.Header    proxyReq.Header = make(http.Header)    for h, val := range req.Header {        proxyReq.Header[h] = val    }    resp, err := httpClient.Do(proxyReq)    if err != nil {        http.Error(w, err.Error(), http.StatusBadGateway)        return    }    defer resp.Body.Close()    // legacy pre}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号