【问题标题】:WCF Restful Service: The body encoding changes automagically?WCF Restful Service:正文编码自动更改?
【发布时间】:2012-07-03 06:57:46
【问题描述】:

我有一个这样的 WCF 服务:

[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>

但是当我使用 fiddler 和 POST 方法调用它时:

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

与身体:

asdasd

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

我的代码是 C#,框架 4,在 VS2010Pro 中构建。

请帮忙。提前致谢。

【问题讨论】:

    标签: c# wcf rest .net-4.0


    【解决方案1】:

    Something 对结果或请求进行 base64 编码。 asdasd 的 ASCII 字节在 base64 编码时输出为 YXNkYXNk

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

    编辑:现在我仔细查看了您的代码,看起来相当清晰。

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

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

    【讨论】:

    • 提供身体有什么不清楚的地方?我使用 Fiddler 并将 asdasd 放在 body 字段中,还是服务器端 关于如何检索正文?请帮忙。
    • @JohnIsaiahCarmona:我面前没有 Fiddler,我不知道有什么选择。但请查看我的编辑 - 基本上 Fiddler 和服务器都将其视为 binary 数据,但您没有从它的 base64 表示解码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2015-03-29
    • 2012-05-29
    • 1970-01-01
    • 2011-07-11
    • 2015-05-10
    • 2016-02-15
    相关资源
    最近更新 更多