在自己电脑能正常访问,在远程服务器上部署发现无法访问微信https网址。
检查了远程服务器的时间也是正常的,具体啥原因就懒得钻研了
测试用exe直接打开的方式部署正常,那么估计问题出在iis的部署方式的配置问题了,和之前遇到的配置跨域无效问题类似。
如果以web.config iis启动 则这么配置
<configuration>
<system.net>
<settings>
<servicePointManager
checkCertificateName="false"
checkCertificateRevocationList="false"
/>
</settings>
</system.net>
</configuration>
代码中忽略证书
ServicePointManager.ServerCertificateValidationCallback += ( sender, cert, chain, sslPolicyErrors ) => true;
可以解决这个WebClient
写法
ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateValidationCallback;
using WebClient MyWebClient = new();
Encoding encode = Encoding.UTF8;
MyWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36");
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
String url = @$"https://baidu.com";
Byte[] pageData = MyWebClient.DownloadData(url);
String result = encode.GetString(pageData);
或者如果是HttpClient则可以细则么忽略
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true; //Is valid
}
/* if (cert.GetCertHashString() == "99E92D8447AEF30483B1D7527812C9B7B3A915A7")
{
return true;
}*/
return true;
};
using (var client = new HttpClient(httpClientHandler))
{
client.DefaultRequestHeaders.Add("User-Agent", "MyUserAgent");
client.DefaultRequestHeaders.Add("Authorization", "Bearer YourAccessToken");
}
}
}
web iis启动配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="coreapi.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</location>
<system.net>
<settings>
<servicePointManager
checkCertificateName="false"
checkCertificateRevocationList="false"
/>
</settings>
</system.net>
</configuration>
参考
https://stackoverflow.com/questions/42712844/ignore-bad-certificate-net-core?rq=3
https://www.coder.work/article/3021872
网友评论