【问题标题】:Digest authentication in Windows Store app using HttpClient (C#)使用 HttpClient (C#) 在 Windows 应用商店应用程序中进行摘要式身份验证
【发布时间】:2015-05-14 14:57:06
【问题描述】:

我为这个问题苦苦挣扎了一周。 我必须在 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();

我做错了什么?

【问题讨论】:

  • 我将那个确切的代码复制到一个项目中,它工作正常(当然 GetAsync 由于无效的 URI 而失败)。
  • 好吧,我现在用的是 VS 2013 而不是 2015RC,它现在可以工作了。 :)

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


【解决方案1】:

为 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 的情况下有效。

希望这会有所帮助。

谢谢, 席德

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多