如何序列化HttpClient对象在windows phone 8?
本文关键字:windows phone 对象 序列化 HttpClient | 更新日期: 2023-09-27 17:51:09
我使用System.Net.Http.HttpClient Object成功登录了一个论坛,并做了很多操作。然后这个对象可能有一些状态,所以我想在再次打开程序时重用这个对象,实现自动登录。我使用Json.net序列化HttpClient对象到IsolatedStorage,但不可用。有人能帮帮我吗?谢谢你! !
//The code for initializing the HttpClient Object
HttpClientHandler handler = new HttpClientHandler();
handler.UseCookies = true;
HttpCliclient = new HttpClient(handler);
我首先使用Json.net序列化对象,得到json字符串:{"DefaultRequestHeaders":],"BaseAddress":空,"超时":"00:01:40","MaxResponseContentBufferSize":2147483647}
然后反序列化json字符串以获得对象,但对象需要再次登录才能进行操作。
using (IsolatedStorageFile storeFolder = IsolatedStorageFile.GetUserStoreForApplication()) {
string jsonData = JsonConvert.SerializeObject(httpclient);
System.Diagnostics.Debug.WriteLine(jsonData);
using (IsolatedStorageFileStream stream = storeFolder.CreateFile(path))
using (StreamWriter writer = new StreamWriter(stream))
writer.Write(jsonData);
}
using (IsolatedStorageFile storeFolder = IsolatedStorageFile.GetUserStoreForApplication()) {
string jsonData;
using (IsolatedStorageFileStream stream = storeFolder.OpenFile(path, FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
jsonData = reader.ReadToEnd();
HttpClient httpclient = JsonConvert.DeserializeObject<HttpClient>(jsonData);
//need login again to do some operations
}
不要尝试序列化HttpClient。它成功的可能性非常小。基本上,您可能能够从中序列化的唯一状态是默认请求头。创建一个HttpClient Factory方法,并序列化出您想要保留的头信息。