想调用 HTTPS WCF 服务。当我们这样调用时:
SmartPayments2SoapClient client = new SmartPayments2SoapClient();
它工作正常。
但在我的场景中,我不想使用默认绑定。它必须以编程方式创建,但在此,我的服务是安全的,意味着它在 HTTPS 上运行。
请建议我可以在其中以编程方式创建绑定的代码。
这是带有自定义绑定的 App.config:
<customBinding>
<binding name="SmartPayments2Soap12">
<textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</textMessageEncoding>
<httpsTransport manualAddressing="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" requireClientCertificate="false" />
</binding>
</customBinding>
我希望将其转换为 C# 程序。我试过这个:
SecurityBindingElement securityElement = SecurityBindingElement.CreateSslNegotiationBindingElement(false);
HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
httpsTransport.AllowCookies = false;
httpsTransport.BypassProxyOnLocal = false;
httpsTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
httpsTransport.MaxBufferSize = 65536;
httpsTransport.MaxBufferPoolSize = 524288;
httpsTransport.MaxReceivedMessageSize = 65536;
httpsTransport.TransferMode = TransferMode.Buffered;
httpsTransport.UseDefaultWebProxy = true;
httpsTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
httpsTransport.ManualAddressing = false;
httpsTransport.AllowCookies = false;
httpsTransport.BypassProxyOnLocal = false;
//httpsTransport.DecompressionEnabled = true;
httpsTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
httpsTransport.KeepAliveEnabled = true;
httpsTransport.ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
httpsTransport.Realm = "";
httpsTransport.TransferMode = TransferMode.Buffered;
httpsTransport.UnsafeConnectionNtlmAuthentication = false;
httpsTransport.UseDefaultWebProxy = true;
CustomBinding binding = new CustomBinding(securityElement, httpsTransport);
EndpointAddress remoteAddress = new EndpointAddress("https://example.com/Payments/Transact2.asmx");
SmartPayments2SoapClient client = new SmartPayments2SoapClient(binding, remoteAddress);