在客户端如何解码Base64binary

时间:2013-04-04 13:26:09

标签: wcf decode

问题描述:    最近,我想用C#请求一个SilverLight页面的数据,我用Fiddle2.0分析了SVC链接地址(服务器是由政府开发的,所以我没有得到服务器数据的格式),fiddle2.0包RAW显示如下:

HTTP/1.1
Content-Type: text/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo/7+uBLGZJdOnOlUBaMnXVoAAAAAICn7WozYJ0KN+6jko+RwyrVVIPQnrURKvLD/P6yJMRUACQAA
SOAPAction: "http:......"
Host: 
Content-Length: 196
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetDataAndIAQI0320 xmlns="http://tempuri.org/"><cityName>北京</cityName><key/></GetDataAndIAQI0320></s:Body></s:Envelope>

在我的程序中,我关心一个C#项目,并使用svcutils生成数据契约类,通过WCF链接地址,我可以获取数据,但WSDL显示一个函数 返回Base64Binary数据,如下所示:

HTTP/1.1 200 OK
Content-Length: 2648
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 04 Apr 2013 13:02:35 GMT
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetDataAndIAQI0320Response xmlns="http://tempuri.org/"><GetDataAndIAQI0320Result>egAAAHic7Z1daxtHFIb/SvBVexHtfM+s2QhcpxcCO3ZwCr0LW1uJF2SpyOu6uTMUEgKhhTZp0jrFtCSF0CZpG0I+oOS/FK8T/4vOrKTE0jbJgs9BGnVBIBmsd0fe2aNn3/PhaK7bjS8tXVje+qy1mayfjtN4rr3WmDvbOPHlRqu9eWoAAGY9TT+fDYLN1fXmRrxZW7O/sdppp914Na11uhcDRogIiA4+bn8x303SZjeJax8tLMz03j+bvFbY3t6ubfPBe2jw6eLCSi56MmlvpnF7tTlTj4rrqEfHXMxiZ63ZmqkbEwU9uW4zBtDLrt3cf/671bRy9Wh+CUCS1RQh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dxdysOX/iv4O0y8PIyZYDBjCoMv8zgkJnyh8xCieGXoZCZu+XGqC4TuNVlt7Ld+xWWldndGt4vY5hYpqYay4azmENF/3IEy3jBLwsLxWViBMvUuPyy0AMAACxj4H4ZrWnOobFMEBQsC/M0Ix6WudsIcCzLk2/AXNb34CbZMOPwWBbiFJe9rlnzgcoMSnFZiFJcpnEMM4WJZQePnxw82cmuPd9/euVg71HFZ+W8cegiM9R0pp5qPONH8Uy+qydTjdKZIQXTLByhMz4uOpt804zWDAKdhQq89N8AAF1d8C2ZVLgsKR6dOUIBhzOpPUhmCg3tmaHUmOFkM7lH4zL+2bnujWmGVWTGMOns8Ofn+8+uHnz/R7b3dYVm5e7CoNEMNaFpphnNXHr5SF/mUEJTvbfSrOCcmVHnjI4roak9YDP4+n9aEwo8oSkYBpvx0PUV4LGZEhj1/4LCz8sAACDgk8xC6ElmCKPMsPoy+7M9vIAzjTEuYzAgDZbNFA6bSVznbGfnYO/64Y0rFZiV2eC+kVk41WQ21APgRo+8ITM9MjCDFkrNWCGnqSekB8CLQWYYOU2hQ3Ay4yiuWT7aA4/MGEYLgBE+zMuAds0wuMzVuP7PZ8wajBmzhqF4ZgwAAIfLUBszD25dy549qris5AaH7810DjYal1EyzVw2nMzMvZHyA2ZVYWLG5Awym/xsJlJvJkI2E372v+UyQjhF7c0kGBNm88J1YDIzDDyfKSafzAzHqTXzaMBsSFBqzXCymThkhpvNzO7e3f/7u/2n97MXX1VwVmbzCPj+TIEJZ9MAAD39f6g/03XLvBXOdMEze382c2yemQ9shpHNVAzeM4Mf/u/6AJjbPoieGUPpA6AEvtZMTfyUWQzXjGgcOJMewRnGOLNBzydweybOkFnk9sxv7lQ1ZiV3t4K3zDQmlU338P+hVKaU76AyU6AyfszuzOCt/72z/i83nNMJAAAAAAAAAA==</GetDataAndIAQI0320Result></GetDataAndIAQI0320Response></s:Body></s:Envelope>

在客户端上,我可以获得这个Base64Binary数据,但是当我解码这段代码时,它会显示混乱的代码。我使用下面的功能:    Convert.tobase64string(bytes)

我想得到帮助,最好给我C#代码。

=============================================== ================================== 嗨,evgenyl,谢谢你的回复!以下是详细代码:

我用工具SVCUTIL.exe生成了generatedProxy.cs,代码如下:

 [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public partial class EnvAQIServeiceClient :       
    System.ServiceModel.ClientBase<IEnvAQIServeice>, IEnvAQIServeice
    {
      ...
      public byte[] GetAllAQIPublish0320()
      {
        return base.Channel.GetAllAQIPublish0320();
      }
      ...
    }

在我的C#程序中,我只有三个陈述:

EnvAQIServeiceClient myclient = new EnvAQIServeiceClient(); 
byte[] Bytes = myclient.GetDataAndIAQI0320("北京", "");
string str=Convert.tobase64string(Bytes)

字符串str值如下:

egAAAHic7Z1daxtHFIb/SvBVexHtfM+s2QhcpxcCO3ZwCr0LW1uJF2SpyOu6uTMUEgKhhTZp0jrFtCSF0CZpG0I+oOS/FK8T/4vOrKTE0jbJgs9BGnVBIBmsd0fe2aNn3/PhaK7bjS8tXVje+qy1mayfjtN4rr3WmDvbOPHlRqu9eWoAAGY9TT+fDYLN1fXmRrxZW7O/sdppp914Na11uhcDRogIiA4+bn8x303SZjeJax8tLMz03j+bvFbY3t6ubfPBe2jw6eLCSi56MmlvpnF7tTlTj4rrqEfHXMxiZ63ZmqkbEwU9uW4zBtDLrt3cf/671bRy9Wh+CUCS1RQhUTC/5PQAAM4zsQ6iSbRxmk6vHjVOA2hKLqKgcboeLcRpkm6tNQE0eVgzSvMoGGha9U77IpQ8parGlbLyA9F6dGaJQewrGQVWKZcDOmeK5pK9M7bEIc6YXeQSd2JQ28rpDRZ43kBIGpVrmoEk1EpJX7a/2u5as9tYg9hSVrcnZsPWIiUAkqEAAL2unFRPEOgPEOqeaO/zLy+y8xJClTpVq9WXhNr6si/bX21nM0mTTvtMvAERBfafXskevnh190X24Dd7nCPi9lDdZCPuXlrutFpb9tsoBTjc4S83X/717cur9z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LgsKR6dOUIBhzOpPUhmCg3tmaHUmOFkM7lH4zL+2bnujWmGVWTGMOns8Ofn+8+uHnz/R7b3dYVm5e7CoNEMNaFpphnNXHr5SF/mUEJTvbfSrOCcmVHnjI4roak9YDP4+n9aEwo8oSkYBpvx0PUV4LGZEhj1/4LCz8sAACDgk8xC6ElmCKPMsPoy+7M9vIAzjTEuYzAgDZbNFA6bSVznbGfnYO/64Y0rFZiV2eC+kVk41WQ21APgRo+8ITM9MjCDFkrNWCGnqSekB8CLQWYYOU2hQ3Ay4yiuWT7aA4/MGEYLgBE+zMuAds0wuMzVuP7PZ8wajBmzhqF4ZgwAAIfLUBszD25dy549qris5AaH7810DjYal1EyzVw2nMzMvZHyA2ZVYWLG5Awym/xsJlJvJkI2E372v+UyQjhF7c0kGBNm88J1YDIzDDyfKSafzAzHqTXzaMBsSFBqzXCymThkhpvNzO7e3f/7u/2n97MXX1VwVmbzCPj+TIEJZ9MAAD39f6g/03XLvBXOdMEze382c2yemQ9shpHNVAzeM4Mf/u/6AJjbPoieGUPpA6AEvtZMTfyUWQzXjGgcOJMewRnGOLNBzydweybOkFnk9sxv7lQ1ZiV3t4K3zDQmlU338P+hVKaU76AyU6AyfszuzOCt/72z/i83nNMJAAAAAAAAAA==

WSDL文件部分描述此响应如下(type =“xs:base64binary”):

<xs:element name="GetDataAndIAQI0320">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="cityName" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="key" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetDataAndIAQI0320Response">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="GetDataAndIAQI0320Result" nillable="true" type="xs:base64Binary"/>
</xs:sequence>
</xs:complexType>
</xs:element>

在互联网上很多人都说:Silverlight页面返回的值是由GZIP压缩,deflate,所以,我发现GZipStream函数解码了值,但代码显示错误:幻数不正确,解码函数如下:

MemoryStream ms = new MemoryStream(Bytes, 0, Bytes.Length);
GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);
string pts = "";
using (StreamReader reader = new StreamReader(gzip))
{
  pts = reader.ReadToEnd();
}

所以,我很困惑。我很乐意帮忙!

1 个答案:

答案 0 :(得分:0)

你确定它的base64? 当我尝试解码它时(在记事本++中) - 我没有收到有效的字符串。

请发布您的代码