将 HTTP 请求/响应标头从调用复制到 HttpWebRequest?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式获取标头
您可以通过以下方式设置标头
示例:
但是,请注意某些标头是受限制的,无法自由修改。它们是:
我想您应该根据具体情况查看您可以/想要从拨入呼叫转至拨出呼叫。
You can get the headers via
You can set the headers via
Example:
However, understand that some headers are restricted, and cannot be modified freely. These are:
I suppose you should look, case by case, which headers you can/want to replicate from the incoming call to the outgoing one.
对于前。您想将所有请求标头复制到 HttpWebRequest 标头:
和实现:
希望它有帮助。
For ex. you want to copy all Request headers to the HttpWebRequest headers:
and implementation:
Hope it helps.