【发布时间】:2016-10-04 12:34:59
【问题描述】:
我正在集成一项服务,当我向以下格式的 URL 发出 GET 请求时会返回一个密钥:
https://username:password@service.com/refresh.key
当我在浏览器中访问 URL 时,它会按预期返回新密钥,当我使用 HttpClient 执行 GET 请求时,我得到一个 401。
HttpClient _client = new HttpClient();
var response = await _client.GetAsync(@"https://username:password@service.com/refresh.key"); // Returns a 401
我认为它与 URL 中的 '@' 有关,但我不确定如何修复它,我尝试将其替换为 '%40',但是当我这样做时,我得到了 UriFormatException .
有人知道怎么做吗?
【问题讨论】:
-
带有
username:password凭据的URI 需要使用基本身份验证转换为请求:baeldung.com/httpclient-4-basic-authentication
标签: c# url dotnet-httpclient .net-4.5.2