2012-07-03 25 views
0

我有一个WCF服务是这样的:WCF Restful Service:正文编码自动更改?

[ServiceContract] 
public class SomeService 
{ 
    [WebInvoke(UriTemplate = "/test", Method = "POST")] 
    public string Test() 
    { 
     using (var reader = OperationContext.Current.RequestContext.RequestMessage.GetReaderAtBodyContents()) 
     { 
      var content = reader.ReadOuterXml().Replace("<Binary>", "").Replace("</Binary>", ""); 
      return content; 
     } 
    } 
} 

而且有一个配置文件是这样的:

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="Project.SomeService"> 
     <endpoint address="" binding="webHttpBinding" contract="Project.SomeService" 
        bindingConfiguration="webHttpBinding_SomeService" behaviorConfiguration="endpointBehavior_SomeService" /> 
     </service> 
    </services> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpBinding_SomeService"> 
      <security mode="None"></security> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="endpointBehavior_SomeService"> 
      <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

但是,当我打电话使用招用这个网址与POST方法是:

http://localhost:1111/SomeService.svc/Test 

与身体:

asdasd 

它会返回YXNkYXNk而不是,为什么会这样?

我的代码是在C#中,框架4,内置VS2010Pro。

请帮忙。提前致谢。

回答

4

Something是base64编码的结果或请求。当base64编码时,asdasd的ASCII字节以YXNkYXNk出现。

目前尚不清楚您如何提供身体,但我建议您使用WireSharkFiddler来查看确切的请求/响应,以确定base64编码的发生位置,然后找出原因并进行修复。

编辑:现在我仔细看了一下你的代码,它似乎很合理清晰。

您的请求旨在包括二进制数据,大概 - 这就是为什么您在XML中有Binary标记。您正在决定忽略这一点,并将二进制数据的XML表示视为文本 - 但您不应该这样做。二进制数据通过base64以XML表示。所以,你应该:

  • 解析XML 作为XML而不是获取外部XML作为一个字符串,然后执行字符串操作
  • 抓取Binary标签的内容作为字符串
  • 使用Convert.FromBase64String得到原始的二进制数据
  • 如果您认为二进制数据最初文本,使用Encoding.GetString将其转换回
+0

什么是不明确的_提供body_?我使用Fiddler并将'asdasd'放在_body_字段中,或者它是服务器端_on如何检索body_?请帮忙。 –

+0

@JohnIsaiahCarmona:在我面前没有提琴手,我不知道有什么选择。但请参阅我的编辑 - 基本上Fiddler和服务器都将它视为*二进制*数据,但是您不是从base64表示中解码出来的。 –

+0

非常感谢!有用。 –