将 HTTP 请求/响应标头从调用复制到 HttpWebRequest?

发布于 2024-11-17 01:47:14 字数 801 浏览 7 评论 0原文

我有一个 C# WCF 服务,它接收请求消息并将其发布到另一个服务。 发布到其他服务是通过 HttpWebRequest 完成的。 当我将它们发布到其他服务时,如何在我的服务中获取原始请求 HTTP 标头并将它们放入 HttpWebRequest 中。

像这样的事情:

HttpRequestMessageProperty httpRequestProp = GetHttpRequestProp(requestMessage);
 HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(uri);
  foreach (var item in httpRequestProp.Headers.AllKeys)
            {

                 loHttp.Headers.Add(item, httpRequestProp.Headers[item]);
            }

我知道这不起作用,因为 HttpWebRequest loHttp 有它自己的属性,当我尝试以上述方式设置 ContentType 时,它​​会抛出异常,因为它需要这样设置:

loHttp.ContentType = httpRequestProp.Headers[HttpRequestHeader.ContentType];

那么有没有办法从调用中复制 HTTP 请求标头并将它们作为 HTTP 请求标头放入另一个 HttpWebRequest ?另外,原始请求可能设置了其他自定义标头,我想将它们也发送到其他服务。

谢谢你, 阿德里亚

I have a C# WCF service that receives a request Message and post it to another service.
Posting to the other service is done through HttpWebRequest.
How can i get in my service the original request HTTP headers and put them in the HttpWebRequest when i post them to the other service.

Something like this:

HttpRequestMessageProperty httpRequestProp = GetHttpRequestProp(requestMessage);
 HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(uri);
  foreach (var item in httpRequestProp.Headers.AllKeys)
            {

                 loHttp.Headers.Add(item, httpRequestProp.Headers[item]);
            }

I know this doesn't work because HttpWebRequest loHttp has its own properties, and when i try to set ContentType for example in the above way it throws exception because it needs to be set like this:

loHttp.ContentType = httpRequestProp.Headers[HttpRequestHeader.ContentType];

So is there a way to copy the HTTP request headers from a call and put them as HTTP request headers to another HttpWebRequest ? Also the original request might have other custom headers set and i want send those also to the other service.

Thank you,
Adrya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2024-11-24 01:47:14

您可以通过以下方式获取标头

OperationContext.Current.RequestContext.RequestMessage.Headers

您可以通过以下方式设置标头

WebClient.Headers

示例:

WebClient wc = new WebClient();
wc.Headers.Add("referer", "http://yourwebsite.com");
wc.Headers.Add("user-agent", "Mozilla/5.0");

但是,请注意某些标头是受限制的,无法自由修改。它们是:

  • 接受
  • 连接
  • 内容长度
  • 内容类型
  • 日期
  • 期望
  • 主机
  • 如果-修改-自范围
  • 引荐来源
  • 网址
  • 传输编码
  • 用户代理
  • 代理连接

我想您应该根据具体情况查看您可以/想要从拨入呼叫转至拨出呼叫。

You can get the headers via

OperationContext.Current.RequestContext.RequestMessage.Headers

You can set the headers via

WebClient.Headers

Example:

WebClient wc = new WebClient();
wc.Headers.Add("referer", "http://yourwebsite.com");
wc.Headers.Add("user-agent", "Mozilla/5.0");

However, understand that some headers are restricted, and cannot be modified freely. These are:

  • Accept
  • Connection
  • Content-Length
  • Content-Type
  • Date
  • Expect
  • Host
  • If-Modified-Since
  • Range
  • Referer
  • Transfer-Encoding
  • User-Agent
  • Proxy-Connection

I suppose you should look, case by case, which headers you can/want to replicate from the incoming call to the outgoing one.

要走就滚别墨迹 2024-11-24 01:47:14

对于前。您想将所有请求标头复制到 HttpWebRequest 标头:

HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
CopyHeaders(httpWebRequest, httpWebRequest.Headers, this.Request.Headers);

和实现:

    void CopyHeaders(object rootTo, NameValueCollection to, NameValueCollection from)
    {
        foreach (string header in from.AllKeys)
        {
            try
            {
                to.Add(header, from[header]);
            }
            catch
            {
                try
                {
                    rootTo.GetType().GetProperty(header.Replace("-", "")).SetValue(rootTo, from[header]);
                }
                catch {}
            }
        }
    }

希望它有帮助。

For ex. you want to copy all Request headers to the HttpWebRequest headers:

HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
CopyHeaders(httpWebRequest, httpWebRequest.Headers, this.Request.Headers);

and implementation:

    void CopyHeaders(object rootTo, NameValueCollection to, NameValueCollection from)
    {
        foreach (string header in from.AllKeys)
        {
            try
            {
                to.Add(header, from[header]);
            }
            catch
            {
                try
                {
                    rootTo.GetType().GetProperty(header.Replace("-", "")).SetValue(rootTo, from[header]);
                }
                catch {}
            }
        }
    }

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文