douyun3887 2017-09-19 10:26
浏览 101
已采纳

获取所有HTTP响应的标头,并在下一个HTTP请求中将其发送回

Go version: go1.8.1 windows/amd64

Sample code for HTTP request is:

func (c *Client) RoundTripSoap12(action string, in, out Message) error {
    fmt.Println("****************************************************************")
    headerFunc := func(r *http.Request) {
        r.Header.Add("Content-Type", fmt.Sprintf("text/xml; charset=utf-8"))
        r.Header.Add("SOAPAction", fmt.Sprintf(action))
        r.Cookies()
    }
    return doRoundTrip(c, headerFunc, in, out)
}

func doRoundTrip(c *Client, setHeaders func(*http.Request), in, out Message) error {
    req := &Envelope{
        EnvelopeAttr: c.Envelope,
        NSAttr:       c.Namespace,
        Header:       c.Header,
        Body:         Body{Message: in},
    }

    if req.EnvelopeAttr == "" {
        req.EnvelopeAttr = "http://schemas.xmlsoap.org/soap/envelope/"
    }
    if req.NSAttr == "" {
        req.NSAttr = c.URL
    }
    var b bytes.Buffer
    err := xml.NewEncoder(&b).Encode(req)
    if err != nil {
        return err
    }
    cli := c.Config
    if cli == nil {
        cli = http.DefaultClient
    }
    r, err := http.NewRequest("POST", c.URL, &b)
    if err != nil {
        return err
    }
    setHeaders(r)
    if c.Pre != nil {
        c.Pre(r)
    }
    fmt.Println("*************", r)
    resp, err := cli.Do(r)
    if err != nil {
        fmt.Println("error occured is as follows ", err)
        return err
    }
    fmt.Println("response headers are: ", resp.Header.Get("sprequestguid"))
    defer resp.Body.Close()
    if resp.StatusCode != http.StatusOK {
        // read only the first Mb of the body in error case
        limReader := io.LimitReader(resp.Body, 1024*1024)
        body, _ := ioutil.ReadAll(limReader)
        return fmt.Errorf("%q: %q", resp.Status, body)
    }
    return xml.NewDecoder(resp.Body).Decode(out)

I will call the RoundTripSoap12 function on the corresponding HTTP client. When I send a request for the first time I will be getting some headers in the HTTP response, so these HTTP response headers should be sent as-is in my next HTTP request.

  • 写回答

1条回答 默认 最新

  • dqeznd1697 2017-09-19 15:33
    关注

    You may be interested in the httputil package and the reverse proxy example provided if you wish to proxy requests transparently:

    https://golang.org/src/net/http/httputil/reverseproxy.go

    You can copy the headers from one request to another one fairly easily - the Header is a separate object, if r and rc are http.Requests and you don't mind them sharing a header (you may need to clone instead if you want independent requests):

    rc.Header = r.Header // note shallow copy
    fmt.Println("Headers", r.Header, rc.Header)
    

    https://play.golang.org/p/q2KUHa_qiP

    Or you can look through keys and values and only copy certain headers, and/or do a clone instead to ensure you share no memory. See the http util package here for examples of this - see the functions cloneHeader and copyHeader inside reverseproxy.go linked above.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大