我已经建立了一个简单的Web API项目,该项目强制执行https,
当我通过浏览器调用服务时,我得到了正确的结果:
但是,如何从WPF客户端调用此服务?
调用“非” https服务不会给我任何问题:
HttpClient client = new HttpClient()
{
BaseAddress = new Uri("http://localhost:49838/")
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
HttpResponseMessage response = await client.GetAsync("/api/products/");
...
我如何调用我的https:// ...服务?
我知道您已经解决了您的问题。Bur我的回答只是为了更新您和其他新用户,他们应该使用HttpClient类而不是WebRequest。
了解有关HTTPClient的更多信息-HttpClient在这里!
HttpClient是功能更强大且经过改进的工具。HttpClient是.NET的现代HTTP客户端。它提供了一种灵活且可扩展的API,用于访问通过HTTP公开的所有内容。
这是将检查来自受信任的根证书颁发机构(StoreName.Root)的证书的示例方法,并将检查本地计算机(StoreLocation.LocalMachine)中的证书。
X509Certificate2 cert = null;
X509Store store = null;
try
{
store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
// You can check certificate here ...
// and populate cert variable..
}
finally
{
if (store != null) store.Close();
}
var clientHandler = new WebRequestHandler();
if (cert != null) clientHandler.ClientCertificates.Add(cert);
var client = new HttpClient(clientHandler) {BaseAddress = new Uri(uri)};
然后您可以做任何您想做的事情。
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句