dqn8235 2014-11-21 16:04
浏览 18
已采纳

在重定向和代理上转到http.Request标头

https://groups.google.com/forum/#!topic/golang-nuts/OwGvopYXpwE

As seen in this thread, when http.Client sends requests to redirects, the header gets reset.

There is a workaround like:

client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
    if len(via) >= 10 {
        return fmt.Errorf("too many redirects")
    }
    if len(via) == 0 {
        return nil
    }
    for attr, val := range via[0].Header {
        if _, ok := req.Header[attr]; !ok {
            req.Header[attr] = val
        }
    }
    return nil
}

But my question is how do I do this if I want to http Request through my proxy server.

When http request goes through proxy server, does header get all reset? Do I have to set up another http.Client in proxy?

I set up my proxy server using https://github.com/elazarl/goproxy

Thanks,

  • 写回答

1条回答 默认 最新

  • doucha4054 2014-11-21 20:05
    关注

    I looked briefly at the code, at it looks like goproxy doesn't handle 3XX responses in any special way – they're simply routed back to your client, so it can react accordingly. In your case, you'll issue another request with all the headers set.

    If it wasn't your concern and you were just wondering if simply existence of proxy requires any hack on the client side, then no, http.Client won't remove any headers when proxy is in use, it won't even know there's a proxy involved.

    You can easily test these assumption by setting up you own a simple server (in Go for example) which is returning a redirect response (to itself or some nc listener) and printing all the headers. When routing the client to the server through your proxy, you can make sure everything looks good on the server side.

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

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败