WCF RESTful服务:该机构编码自动地改变? [英] WCF Restful Service: The body encoding changes automagically?

查看:110
本文介绍了WCF RESTful服务:该机构编码自动地改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务是这样的:

I have a WCF Service like this:

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

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

And has a config file like this:

<?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 方法是:


But when I call it using fiddler with this url with POST method:

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

与身体:

asdasd

返回 YXNkYXNk 代替,为什么这样说?

it returns YXNkYXNk instead, why was it like this?

我的code是在C#中,框架4,打造VS2010Pro。

My Code is in C#, framework 4, build in VS2010Pro.

请帮忙。先谢谢了。

推荐答案

的东西的都是Base64编码无论是结果或请求。在ASCII字节 asdasd 出来为 YXNkYXNk 时的base64恩codeD。

Something is base64-encoding either the result or the request. The ASCII bytes for asdasd come out as YXNkYXNk when base64-encoded.

目前还不清楚你是如何提供的身体,但我建议你看一下确切的请求/响应使用是Wireshark 提琴手来在哪里工作中的Base64编码正在发生的事情,然后制定出原因,然后解决它。

It's not clear how you're providing the body, but I suggest you look at the exact request/response using WireShark or Fiddler to work out where the base64-encoding is happening, then work out why, then fix it.

编辑:现在我已经仔细看看你的code,它似乎相当清楚

Now I've had a closer look at your code, it seems reasonably clear.

您的要求是指包括的的数据,presumably - 这就是为什么你已经得到了XML中的二进制标签。你决定忽略这一点,只把二进制数据的XML重新presentation为文本 - 但你不应该。二进制数据是通过BASE64 psented在XML重新$ P $。所以,你应该:

Your request is meant to include binary data, presumably - which is why you've got a Binary tag within the XML. You're deciding to ignore that, and just treat the XML representation of the binary data as text - but you shouldn't. Binary data is represented in XML via base64. So, you should:

  • 解析XML的为XML 的,而不是获取外部XML作为一个字符串,然后执行字符串操作
  • 抓取二进制标签的内容作为字符串
  • 使用 Convert.FromBase64String 来获得原始二进制数据
  • 如果您认为的二进制数据最初文本,使用 Encoding.GetString 将其转换回
  • Parse the XML as XML rather than getting the outer XML as a string and then performing string operations
  • Fetch the contents of the Binary tag as a string
  • Use Convert.FromBase64String to get the original binary data
  • If you believe that binary data was originally text, use Encoding.GetString to convert it back

这篇关于WCF RESTful服务:该机构编码自动地改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆