2014-04-25 38 views
0

在通過帖子調用wcf服務時,丟失了xml內容。我只看到「<」wcf方法。我是這樣傳遞XML內容的:samplesample。當向wcf REST發佈POST時丟失xml內容

客戶:

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!

更新時間:

同時呼籲從httpwebresponse服務,我得到以下錯誤:

The server encountered an error processing the request. The exception message is 'Incoming message for operation 'ParseXML' (contract 'IXmlParseService' with namespace ' http://tempuri.org/ ') contains an unrecognized http body format value 'Xml'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is:

at System.ServiceModel.Dispatcher.HttpStreamFormatter.GetStreamFromMessage(Message message, Boolean isRequest) at System.ServiceModel.Dispatcher.HttpStreamFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

回答

0

你的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) { 
    } 
}); 
+0

如果我通過Ajax調用,它的工作原理,即使沒有encodeURIComponent方法。請參閱原始問題 – user1480864

+0

中更新的錯誤消息您是否在服務配置中定義了xml的正確WebHttpBinding和服務行爲? –