我无法使用WebRequestCreator.BrowserHttp
连接到该URL,但是我可以使用WebRequestCreator.ClientHttp
连接。这是我正在使用的代码示例,
var httpClient = new HttpClient();
WebRequest.RegisterPrefix("http://", WebRequestCreator.BrowserHttp);
var byteArray = Encoding.UTF8.GetBytes("username:password");
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
我试图避免使用“ Windows安全性”对话框,并且我不能在项目中使用
WebRequestCreator.BrowserHttp
。编辑:
当使用
WebRequestCreator.BrowserHttp
我得到System.ArgumentException:值不在预期范围内
范围
提琴手一无所有。如果我使用
WebRequestCreator.ClientHttp
Authorization: Basic
在提琴手
最佳答案
我可以在当前的Silverlight项目中同时进行基本身份验证和承载(JWT)身份验证。
/*
* NOTE:
* It turns out that Silverlight provides HTTP handling in both the Browser and Client stack.
* By default Silverlight uses the Browser for HTTP handling.
* The only problem with this is that Browser HTTP Handling only supports GET and POST request methods.
*/
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
var httpClient = new HttpClient();
var byteArray = Encoding.UTF8.GetBytes("username:password");
// Default headers will be sent with every request sent from this HttpClient instance.
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);