WebHttpBinding 绑定中使用的默认标头内容类型值

发布于 2024-12-10 02:57:27 字数 160 浏览 0 评论 0原文

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

I'm trying to POST to a REST service using the default WebHttpBinding binding. The service only accepts "text/xml" as the content-type and the WebHttpBinding is sending "application/xml, charset-utf=8". Is there a way to change the default content type without using the the HttpWebRequest?

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

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

发布评论

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

评论(1

此生挚爱伱 2024-12-17 02:57:27

您可以在操作范围内使用 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();
    }
}

You can use the WebOperationContext inside an operation scope to change the outgoing content type of the requests, as shown below.

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