gpt4 book ai didi

c# - WebHttpBinding 绑定(bind)中使用的默认 header 内容类型值

转载 作者:行者123 更新时间:2023-11-30 21:11:58 26 4
gpt4 key购买 nike

我正在尝试使用默认的 WebHttpBinding 绑定(bind) POST 到 REST 服务。该服务仅接受“text/xml”作为内容类型,WebHttpBinding 正在发送“application/xml,charset-utf=8”。有没有一种方法可以在不使用 HttpWebRequest 的情况下更改默认内容类型?

最佳答案

您可以在操作范围内使用 WebOperationContext 来更改请求的传出内容类型,如下所示。

public class StackOverflow_7771645
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Process();
}
public class Service : ITest
{
public string Process()
{
return "Request content type: " + WebOperationContext.Current.IncomingRequest.ContentType;
}
}
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<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
ITest proxy = factory.CreateChannel();
using (new OperationContextScope((IContextChannel)proxy))
{
WebOperationContext.Current.OutgoingRequest.ContentType = "text/xml";
Console.WriteLine(proxy.Process());
}

using (new OperationContextScope((IContextChannel)proxy))
{
WebOperationContext.Current.OutgoingRequest.ContentType = "application/xml";
Console.WriteLine(proxy.Process());
}

((IClientChannel)proxy).Close();
factory.Close();

Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}

关于c# - WebHttpBinding 绑定(bind)中使用的默认 header 内容类型值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com