Micro Framework 如何调用Web Service 上提供数据的方法
通过上一篇在Micro Framework中使用 Socket 调用Web Service文章,我已经很清楚如何调用Web Service 上的方法,但是今天当我调用的方法,其返回的类型为数据类型byte[] 时,使用上一篇文章中方法得到String类型的值后,再转换成byte[],就会出现两个byte数组大小不一致的问题,究竟是问题出现在哪了?
首先要从服务上开始查找原因,重新打开服务点击Invoke调用该服务,其返回值如下:
Code
<?xml version="1.0" encoding="utf-8" ?>
<base64Binary xmlns="http://tempuri.org/">R0lGODlhXwBSAPcAAAAAAP///5AgKasaK5UuPHtHT7ooRdxUcP6asv72/v76/vr6/ur+/vr+/vX9/PT89Pr++nObXpK+eilEFyFHBzBSF32gY3SVXBw5BW6ZTT1jHEVsJEFZLJW9coWia5a0e5Oue6G3ju/… …IADs=</base64Binary>
<?xml version="1.0" encoding="utf-8" ?>
<base64Binary xmlns="http://tempuri.org/">R0lGODlhXwBSAPcAAAAAAP///5AgKasaK5UuPHtHT7ooRdxUcP6asv72/v76/vr6/ur+/vr+/vX9/PT89Pr++nObXpK+eilEFyFHBzBSF32gY3SVXBw5BW6ZTT1jHEVsJEFZLJW9coWia5a0e5Oue6G3ju/… …IADs=</base64Binary>
通过观察,很容易发现原因,就是通过 Web Service的传过来的数据是经过Base64编过码的,那么只要使用Convert.FromBase64String转过来就行了吗,哈哈!
但是很遗憾,Micro Framework中已经不存在这个方法了,那么就要重新实现这个方法了。该方法实现如下:
Code
public class Base64Util
{
static public byte[] GetDecoded(string strInput)
{
char[] source;
int length, length2, length3;
int blockCount;
int paddingCount;
char[] input = strInput.ToCharArray();
int temp = 0;
source = input;
length = input.Length;
for (int x = 0; x < 2; x++)
{
if (input[length - x - 1] == '=')
temp++;
}
paddingCount = temp;
blockCount = length / 4;
length2 = blockCount * 3;
byte[] buffer = new byte[length];
byte[] buffer2 = new byte[length2];
for (int x = 0; x < length; x++)
{
buffer[x] = char2sixbit(source[x]);
}
byte b, b1, b2, b3;
byte temp1, temp2, temp3, temp4;
for (int x = 0; x < blockCount; x++)
{
temp1 = buffer[x * 4];
temp2 = buffer[x * 4 + 1];
temp3 = buffer[x * 4 + 2];
temp4 = buffer[x * 4 + 3];
b = (byte)(temp1 << 2);
b1 = (byte)((temp2 & 48) >> 4);
b1 += b;
b = (byte)((temp2 & 15) << 4);
b2 = (byte)((temp3 & 60) >> 2);
b2 += b;
b = (byte)((temp3 & 3) << 6);
b3 = temp4;
b3 += b;
buffer2[x * 3] = b1;
buffer2[x * 3 + 1] = b2;
buffer2[x * 3 + 2] = b3;
}
length3 = length2 - paddingCount;
byte[] result = new byte[length3];
for (int x = 0; x < length3; x++)
{
result[x] = buffer2[x];
}
return result;
}
private static byte char2sixbit(char c)
{
char[] lookupTable = new char[64]{
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X','Y', 'Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'};
if (c == '=')
return 0;
else
{
for (int x = 0; x < 64; x++)
{
if (lookupTable[x] == c)
return (byte)x;
}
return 0;
}
}
}
public class Base64Util
{
static public byte[] GetDecoded(string strInput)
{
char[] source;
int length, length2, length3;
int blockCount;
int paddingCount;
char[] input = strInput.ToCharArray();
int temp = 0;
source = input;
length = input.Length;
for (int x = 0; x < 2; x++)
{
if (input[length - x - 1] == '=')
temp++;
}
paddingCount = temp;
blockCount = length / 4;
length2 = blockCount * 3;
byte[] buffer = new byte[length];
byte[] buffer2 = new byte[length2];
for (int x = 0; x < length; x++)
{
buffer[x] = char2sixbit(source[x]);
}
byte b, b1, b2, b3;
byte temp1, temp2, temp3, temp4;
for (int x = 0; x < blockCount; x++)
{
temp1 = buffer[x * 4];
temp2 = buffer[x * 4 + 1];
temp3 = buffer[x * 4 + 2];
temp4 = buffer[x * 4 + 3];
b = (byte)(temp1 << 2);
b1 = (byte)((temp2 & 48) >> 4);
b1 += b;
b = (byte)((temp2 & 15) << 4);
b2 = (byte)((temp3 & 60) >> 2);
b2 += b;
b = (byte)((temp3 & 3) << 6);
b3 = temp4;
b3 += b;
buffer2[x * 3] = b1;
buffer2[x * 3 + 1] = b2;
buffer2[x * 3 + 2] = b3;
}
length3 = length2 - paddingCount;
byte[] result = new byte[length3];
for (int x = 0; x < length3; x++)
{
result[x] = buffer2[x];
}
return result;
}
private static byte char2sixbit(char c)
{
char[] lookupTable = new char[64]{
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X','Y', 'Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'};
if (c == '=')
return 0;
else
{
for (int x = 0; x < 64; x++)
{
if (lookupTable[x] == c)
return (byte)x;
}
return 0;
}
}
}
有了这个方法只要使用上一篇文章中的方法得到string 类型,再使用Base64Decoder.GetDecoded(str)方法即可得到Web Service 传过来的byte[]数据了。