使用HttpClient(C#)在Windows应用商店应用中进行摘要式身份验证

时间:2015-05-14 14:57:06

标签: c# windows-phone windows-store-apps dotnet-httpclient

我正在努力解决这个问题一个星期。 我必须在Windows应用商店应用中使用带摘要式身份验证的API,但是当我使用这段代码时,我在这行代码中得到了System.ArgumentNullException:

HttpHandler.Credentials = credCache;

以下是代码的其余部分:

var credCache = new CredentialCache();
credCache.Add(new Uri("https://myserverIP/api"),"Digest",new NetworkCredential("mylogin", "mypassword") );
var HttpHandler = new HttpClientHandler();
HttpHandler.Credentials = credCache;
var httpClient = new HttpClient(HttpHandler);
var answer = await httpClient.GetAsync(new Uri("https://myserverIP/api/?function=someKindOfFunction"));
answer.EnsureSuccessStatusCode();

我做错了什么?

1 个答案:

答案 0 :(得分:0)

快速解决您的问题是在为HttpHandler分配值时使用credCache.GetCredentials()而不是credCache。证书如此:

var credCache = new CredentialCache();
credCache.Add(new Uri("https://myserverIP/api"),"Digest",new NetworkCredential("mylogin", "mypassword") );
var HttpHandler = new HttpClientHandler();
HttpHandler.Credentials = credCache.GetCredential(new Uri("https://myserverIP/api"), "Digest");

这不适用于ArgumentNullException

希望这有帮助。

谢谢, SID