我正在尝试将我的winform c#deskltop移植到uwp。
在我的代码的这一部分中,我调用web api来返回序列化列表。
它也被压缩了。
这是现有的代码:
Uri uri = new Uri(URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsByteArrayAsync().Result;
string result2 = System.Text.Encoding.ASCII.GetString(result);
byte[] actualBytes = Convert.FromBase64String(result2.Replace("\"",""));
var myobj= Compression.DeSerialize(actualBytes);
我的压缩代码:
public static byte[] Decompress(byte[] input)
{
byte[] decompressedData;
using (var outputStream = new MemoryStream())
{
using (var inputStream = new MemoryStream(input))
{
using (var zip = new GZipStream(inputStream, CompressionMode.Decompress))
{
zip.CopyTo(outputStream);
}
}
decompressedData = outputStream.ToArray();
}
return decompressedData;
}
public static Object DeSerialize(this byte[] arrBytes)
{
using (var memoryStream = new MemoryStream())
{
var binaryFormatter = new BinaryFormatter();
var decompressed = Decompress(arrBytes);
memoryStream.Write(decompressed, 0, decompressed.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
return binaryFormatter.Deserialize(memoryStream);
}
}
和我的模特:
[Serializable, XmlRoot("Groups"), XmlType("Groups")]
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlElement("Group")]
public List<Group> group { get; set; }
}
,XmlRoot(“Group”),XmlType(“Group”)] 公共班组 { [XmlElement的( “GroupRef”)] 公共Guid GroupRef; [XmlElement的( “名称”)] 公共字符串名称; [XmlElement的( “描述”)] public string描述; }
现在把它移植到UWP我有这个:
Uri uri = new Uri(Shared.URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/text"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + GeneralTags.COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsByteArrayAsync().Result;
string result2 = System.Text.Encoding.ASCII.GetString(result);
byte[] actualBytes = Convert.FromBase64String(result2.Replace("\"", ""));
var myobj = Compression.DeSerialize<InformedWorker.Models.Group>(actualBytes);
我的压缩代码:
public static byte[] Compress(byte[] input)
{
byte[] compressesData;
using (var outputStream = new MemoryStream())
{
using (var zip = new GZipStream(outputStream, CompressionMode.Compress))
{
zip.Write(input, 0, input.Length);
}
compressesData = outputStream.ToArray();
}
return compressesData;
}
public static T DeSerialize<T>(byte[] arrBytes)
{
var decompressed = Decompress(arrBytes);
using (MemoryStream memoryStream = new MemoryStream(decompressed))
{
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memoryStream, XmlDictionaryReaderQuotas.Max))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(T));
return (T)dcs.ReadObject(reader);
}
}
}
和我的模特:
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlElement("Group")]
public List<Group> group { get; set; }
}
public class Group
{
[XmlElement("GroupRef")]
public Guid GroupRef;
[XmlElement("Name")]
public string Name;
[XmlElement("Description")]
public string Description;
}
请注意: 我不被允许使用'[Serializable~'所以我必须删除。
另外,我找不到BinaryFormatter,所以不得不使用'DataContractSerializer'。
这行错误:
return (T)dcs.ReadObject(reader);
,错误是:
There was an error deserializing the object of type The input source is not correctly formatted.
我正在使用这种方法,以便我可以将数据从服务器压缩到客户端。
有关如何使其发挥作用的任何想法吗?
感谢
附加:
这是我正在从服务器压缩/发送的XML:
<Groups>
<Group>
<GroupId>1</GroupId>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>Todays Work</Name>
<Description>System</Description>
<CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>
<Active>1</Active>
</Group>
<Group>
<GroupId>2</GroupId>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>All</Name>
<Description>System</Description>
<CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>
<Active>1</Active>
</Group>
</Groups>
答案 0 :(得分:1)
等待ReadAsByteArraysAsync。在
中读取数据之前,代码不会继续到下一行var result = await response.Content.ReadAsByteArrayAsync();