DotNetCore调用Https

本人最近碰到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);
}

积极分享,分享人多了就成生态了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小乌鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值