[英]WebChannelFactory & Headers?
是否可以在WebChannelFactory
上設置標頭? 如果我使用的是WebClient
對象,我可以這樣做:
WebClient client = new WebClient();
client.Headers.Add("referer", "http://stackoverflow.com");
client.Headers.Add("user-agent", "Mozilla/5.0");
但我還沒有找到一種方法來修改WebChannelFactory
上的WebChannelFactory
。
WebChannelFactory
類本身不接受任何HTTP標頭,但您可以將它們添加到當前的WebOperationContext
, WebOperationContext
是您要為其創建新的作用域 - 請參閱下文。
WebChannelFactory<ICalculator> factory = new WebChannelFactory<ICalculator>(new Uri(baseAddress));
ICalculator proxy = factory.CreateChannel();
using (new OperationContextScope((IContextChannel)proxy))
{
WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com");
WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0");
Console.WriteLine("Add: {0}", proxy.Add(33, 55));
Console.WriteLine();
}
using (new OperationContextScope((IContextChannel)proxy))
{
WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com");
WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0");
Console.WriteLine("Subtract: {0}", proxy.Subtract(44, 33));
Console.WriteLine();
}
這可行,但它相當冗長 - 如果要為其添加傳出標頭,您基本上需要為每個調用創建一個新范圍。
另一種選擇是將客戶端包裝在客戶端類中,以便為您添加作用域和標題。 使用從ClientBase<T>
派生的類是一種簡單的方法。 下面的代碼是這個問題的完整示例,兩個選項(使用范圍直接,使用客戶端基礎派生類)在WebChannelFactory
創建的代理的請求中添加HTTP頭。
public class StackOverflow_10388746
{
[ServiceContract]
public interface ICalculator
{
[WebGet]
int Add(int x, int y);
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Subtract(int x, int y);
}
public class Service : ICalculator
{
public int Add(int x, int y)
{
PrintHeaders("Add");
return x + y;
}
public int Subtract(int x, int y)
{
PrintHeaders("Subtract");
return x - y;
}
void PrintHeaders(string operation)
{
Console.WriteLine("Incoming HTTP headers for operation '{0}'", operation);
foreach (var header in WebOperationContext.Current.IncomingRequest.Headers.AllKeys)
{
Console.WriteLine(" {0}: {1}", header, WebOperationContext.Current.IncomingRequest.Headers[header]);
}
}
}
public class MyWebClient : ClientBase<ICalculator>, ICalculator
{
Dictionary<string, string> outgoingHeaders = new Dictionary<string, string>();
public MyWebClient(Uri baseAddress)
: base(new WebHttpBinding(), new EndpointAddress(baseAddress))
{
this.Endpoint.Behaviors.Add(new WebHttpBehavior());
}
#region ICalculator Members
public int Add(int x, int y)
{
using (new OperationContextScope(this.InnerChannel))
{
foreach (var headerName in this.outgoingHeaders.Keys)
{
WebOperationContext.Current.OutgoingRequest.Headers.Add(headerName, this.outgoingHeaders[headerName]);
}
this.outgoingHeaders.Clear();
return this.Channel.Add(x, y);
}
}
public int Subtract(int x, int y)
{
using (new OperationContextScope(this.InnerChannel))
{
foreach (var headerName in this.outgoingHeaders.Keys)
{
WebOperationContext.Current.OutgoingRequest.Headers.Add(headerName, this.outgoingHeaders[headerName]);
}
this.outgoingHeaders.Clear();
return this.Channel.Subtract(x, y);
}
}
#endregion
public void AddOutgoingHeader(string name, string value)
{
this.outgoingHeaders.Add(name, value);
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebChannelFactory<ICalculator> factory = new WebChannelFactory<ICalculator>(new Uri(baseAddress));
ICalculator proxy = factory.CreateChannel();
using (new OperationContextScope((IContextChannel)proxy))
{
WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com");
WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0");
Console.WriteLine("Add: {0}", proxy.Add(33, 55));
Console.WriteLine();
}
using (new OperationContextScope((IContextChannel)proxy))
{
WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com");
WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0");
Console.WriteLine("Subtract: {0}", proxy.Subtract(44, 33));
Console.WriteLine();
}
MyWebClient client = new MyWebClient(new Uri(baseAddress));
client.AddOutgoingHeader("referer", "http://stackoverflow.com");
client.AddOutgoingHeader("user-agent", "Mozilla/5.0");
Console.WriteLine("Add (via client): {0}", client.Add(44, 77));
Console.WriteLine();
client.AddOutgoingHeader("referer", "http://stackoverflow.com/another");
client.AddOutgoingHeader("user-agent", "Mozilla/5.0-b");
Console.WriteLine("Add (via client): {0}", client.Subtract(44, 77));
Console.WriteLine();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.