本人最近碰到DotNet和DotNetCore调用Https的情况。DotNetCore调用Https的资料特别少,试了两天才试好,分享出来给后面人避坑。用VS生成Webservice代理类的事就不说了额。
DotNetCore片段
BasicHttpBinding bind1 = new BasicHttpBinding();
if (currentFullFilename.ToLower().Contains("https://"))
{
//类似浏览器确认证书合法方法的绑定
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
bind1 = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
}
bind1.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
bind1.ReaderQuotas.MaxArrayLength = int.MaxValue;
bind1.ReaderQuotas.MaxStringContentLength = int.MaxValue;
bind1.MaxReceivedMessageSize = int.MaxValue;
EndpointAddress endpointAddress = new EndpointAddress(GetFileServiceAddr(currentFullFilename));
UpFileServiceSoapClient client = new UpFileServiceSoapClient(bind1, endpointAddress);
client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication =
new X509ServiceCertificateAuthentication()
{
CertificateValidationMode = X509CertificateValidationMode.None,
RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck,
};
string ret = client.ReNameAsync(currentFullFilename, newFullFilename).Result.Body.ReNameResult;
DotNet片段
BasicHttpBinding bind1 = new BasicHttpBinding();
if (serverPath.ToLower().Contains("https://"))
{
//类似浏览器确认证书合法方法的绑定
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
bind1 = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
}
bind1.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
bind1.ReaderQuotas.MaxArrayLength = int.MaxValue;
bind1.ReaderQuotas.MaxStringContentLength = int.MaxValue;
bind1.MaxReceivedMessageSize = int.MaxValue;
EndpointAddress endpointAddress = new EndpointAddress(GetFileServiceAddr(serverPath));
UpFileServiceSoapClient client = new UpFileServiceSoapClient(bind1, endpointAddress);
string ret = client.ReName(serverPath + remotePath + currentFilename, serverPath + remotePath + newFilename);
这里有个坑,如果是DotNetCore用SOAPCore发布的webservice用上面DotNet代码调用没问题,用上面DotNetCore调用的话传的参数长度超过8000多会报MaxStringContentLength超过设置,开始一直以为是客户端调用代码的事,各种设置MaxStringContentLength无效,后面发现是发布方需要设置MaxStringContentLength
所以发布webservice时候应该明确设定最大长度
/// <summary>
/// 绑定终节点
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="routes"></param>
/// <param name="path"></param>
/// <returns></returns>
public IEndpointConventionBuilder UseSoapEndpoint<T>(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string path)
{
BasicHttpBinding bind1 = new BasicHttpBinding();
bind1.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
bind1.ReaderQuotas.MaxArrayLength = int.MaxValue;
bind1.ReaderQuotas.MaxStringContentLength = int.MaxValue;
bind1.MaxReceivedMessageSize = int.MaxValue;
return routes.UseSoapEndpoint<T>(path, bind1);
}
积极分享,分享人多了就成生态了