POST到wcf REST时正在释放xml内容

本文关键字:释放 xml 内容 wcf REST POST | 更新日期: 2023-09-27 18:00:06

am在通过post调用wcf服务时丢失了xml内容。我只看到wcf方法的"<"。我传递的xml内容如下:samplesample。

客户端:

    jQuery.support.cors = true;
    $.ajax({
        type: 'POST',
        url: 'http://localhost:48677/MYSVC.svc/ParseXML/<?xml version=1.0 encoding=UTF-8?><node>sample</node><node1>sample</node1>',
        cache: false,
        contentType: 'text/xml;charset=UTF-8',
        dataType: 'json',
        processData: true,
        success: function (msg) {
        },
        error: function (err) {
        }
    });

服务接口:

 [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "ParseXML/{xmlContent}")]
    Stream ParseXML(string xmlContent);  

还包括包装,按照:http://vivekcek.wordpress.com/2012/06/14/wcf-rest-service-accepting-raw-xml-webcontenttypemapper/

如果我传递非xml字符串,我的服务会被调用,但不是正确的xml内容。

还包括以下两个服务线路&asp.net web.config:

<httpRuntime requestValidationMode="2.0" maxUrlLength="4096" requestPathInvalidCharacters="" useFullyQualifiedRedirectUrl="true" maxRequestLength="2147483647" requestLengthDiskThreshold="2147483647" executionTimeout="18000" targetFramework="4.5"/>

我也尝试过:requestPathInvalidCharacters="<,>,*,%,:,'',?",但对没有帮助

请指导我如何将正确的xml内容传递给wcf!

更新:

WHile从httpwebresponse调用服务,我得到以下错误:

服务器在处理请求时遇到错误。例外情况消息是"操作的传入消息"ParseXML"(约定具有命名空间的"IXmlParseService"http://tempuri.org/')包含无法识别的http正文格式值"Xml"。预期的正文格式值为"Raw"。这可能是因为WebContentTypeMapper没有已在绑定上配置。请参阅的文档有关详细信息,请参阅WebContentTypeMapper。'。有关详细信息,请参阅服务器日志详细信息。异常堆栈跟踪为:

在System.ServiceModel.Dispatcher.HttpStreamFormatter.GetStreamFromMessage(消息消息,布尔isRequest)System.ServiceModel.Dispatcher.HttpStreamFormatter.DescializeRequest(消息消息,Object[]参数)System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DescializeRequest(消息消息,Object[]参数)System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DescializeRequest(消息消息,Object[]参数)System.ServiceModel.Dispatcher.DispatchOperationRuntime.DescializeInputs(MessageRpc&rpc)System.ServiceModel.Dispatcher.DispatchOperationRuntime.IInvokeBegin(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&rpc)在System.ServiceModel.Dispatcher.MessageRpc.Process(布尔isOperationContextSet)

POST到wcf REST时正在释放xml内容

您的xml没有正确编码以供url使用。您可以使用encodeURIComponent()对内容进行编码。

var payload=encodeURIComponent('<?xml version=1.0 encoding=UTF-8?><node>sample</node><node1>sample</node1>');
$.ajax({
    type: 'POST',
    url: 'http://localhost:48677/MYSVC.svc/ParseXML/'+payload,
    data:{'xmlContent':payload},
    cache: false,
    contentType: 'text/xml;charset=UTF-8',
    dataType: 'json',
    processData: true,
    success: function (msg) {
    },
    error: function (err) {
    }
});