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 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案