我有一个.NET Core Controller API。
通过安全链接(即https:/// api / values)访问Web方法时,它不起作用
如果我使用不安全的链接(即http:/// api / values)访问它,则工作正常。
请告知我是否需要对Startup.cs或appsettings.json进行一些设置
访问API的示例代码:
try
{
string serviceUrl= "https://domainname/api/values";
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(serviceUrl).Result;
string stringData = response.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
throw ex;
}
答案 0 :(得分:1)
添加ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
解决了问题,我从.NET Core API中获取了数据。
注意 :我在通过Miscrosoft Word VSTO-AddIn访问.NET Core Controller API时遇到问题
try
{
/*This is the Line I added and it makes my .NET Core API Accessible in my VSTO-AddIn */
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string serviceUrl= "https://domainname/api/values";
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(serviceUrl).Result;
string stringData = response.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
throw ex;
}