我正在尝试使用摘要式身份验证与服务器通信。我有以下代码。这在Windows 8.1中运行良好,但在Windows Phone中,我会得到"NullReferenceException:对象引用未设置为对象实例"。这似乎与设置凭据有关。如果我不设置它,我只会从服务器得到一个失败的身份验证响应。关于如何解决这个问题有什么想法吗?
var uri = new Uri(url);
var cache = new CredentialCache();
cache.Add(uri, "Digest", new NetworkCredential(Username, Password));
HttpClient client = new HttpClient(new HttpClientHandler { Credentials = cache });
return await client.GetStringAsync(uri);
Windows phone在using Windows.Web.Http;
中有自己的Web客户端
只需将其用于windows phone,创建一个cleint就会像这样工作:
var filter = new HttpBaseProtocolFilter
{
AllowUI = false,
ServerCredential =
new PasswordCredential(
"Your base url",
"Your user name",
"Your password")
};
var httpClient = new HttpClient(filter);
我在一个windows phone应用程序中使用它来通过摘要身份验证对网络进行身份验证。因此,这也应该适用于您的情况。