"The main connection was closed: an unexpected error occurred while sending." (VB.NET Client for Java / Apache SSL-encrypted web service)

I really hope someone here can help. Let me also preface my question by saying that I am not a .NET expert and have very little knowledge of web services and SSL / security issues.

I am trying to create a VB.NET console application to use a Java-based web service hosted on an Apache server. Communication is via SSL. A client certificate is required (provided by web service providers), and basic authentication (username / password) is also required to use the service.

In the installed certificate (using certmgr.mgr) from the provided PFX file and the certificate is stored in both my personal storage and the Trusted Root storage. I did not check all the boxes for strong encryption (used by default for everyone).

I created a proxy class for the service using the provided WSDL file. The WSDL that resides on their server is incorrect and cannot be used for early binding in .NET. I created a proxy class using "SVCUTIL * .wsdl / language: VB".

I get the following error when trying to call one of the public methods from the service:

An error occurred while sending the HTTP request to "https: // webservice-url? WSDL". This may be because the server certificate is not configured properly with HTTP.SYS in the case of HTTPS. This can also be caused by a mismatch in the security binding between the client and server.

If I look at an InnerException from an Exception snapshot, I see the following:

The connected connection was closed: an unexpected error occurred while sending.

Here is the code I used to initialize the client and connect to the web service:

'Override server certificate callback Dim oCertOverride As New CertificateOverride ServicePointManager.ServerCertificateValidationCallback = _ AddressOf oCertOverride.RemoteCertificateValidationCallback 'Set WS binding Dim binding As WSHttpBinding = New WSHttpBinding binding.Security.Mode = SecurityMode.Transport binding.Security.Transport.ClientCredentialType = _ HttpClientCredentialType.Certificate 'Set endpoint address Dim address As EndpointAddress = _ New EndpointAddress("https://webservice-url?WSDL") 'Create web service client Dim ws As wsclient = New wsclient(binding, address) 'Set web service client certificate ws.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, _ StoreName.My, X509FindType.FindBySubjectName, "cert-subject-name") 'Set username and password for server authentication ws.ClientCredentials.UserName.UserName = "username" ws.ClientCredentials.UserName.Password = "password" 'Make test call to web service ws.HelloWord() 

I should also note that I can connect to the web service and view all public methods using Firefox and IE.

I tried the internet for help. Most quick fixes that worked for people did not help. I played with the bind settings, but this led to various errors in the fact that you cannot connect to the web service using the Anonymous User account.

I am really at a loss. Any advice or help would be greatly appreciated.

Thank you for your time.

+4
source share
1 answer

Your binding seems complicated (with certificate setup.)

Try creating this as a class:

 Imports System.ServiceModel Public Class MyBinding Public Property _Binder As BasicHttpBinding Public Sub New(ByVal BinderName As String) _Binder = New BasicHttpBinding() _Binder.Name = BinderName _Binder.CloseTimeout = TimeSpan.FromMinutes(10) _Binder.OpenTimeout = TimeSpan.FromMinutes(10) _Binder.ReceiveTimeout = TimeSpan.FromMinutes(10) _Binder.SendTimeout = TimeSpan.FromMinutes(10) _Binder.AllowCookies = False _Binder.BypassProxyOnLocal = False _Binder.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard _Binder.MaxBufferSize = 655360 _Binder.MaxBufferPoolSize = 655360 _Binder.MaxReceivedMessageSize = 655360 _Binder.MessageEncoding = WSMessageEncoding.Text _Binder.TextEncoding = System.Text.Encoding.UTF8 _Binder.TransferMode = TransferMode.Buffered _Binder.UseDefaultWebProxy = True _Binder.ReaderQuotas.MaxDepth = 32 _Binder.ReaderQuotas.MaxStringContentLength = 8192 _Binder.ReaderQuotas.MaxArrayLength = 655360 _Binder.ReaderQuotas.MaxBytesPerRead = 4096 _Binder.ReaderQuotas.MaxNameTableCharCount = 16384 _Binder.Security.Transport.ClientCredentialType = HttpClientCredentialType.None _Binder.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None _Binder.Security.Transport.Realm = "" _Binder.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName _Binder.Security.Message.AlgorithmSuite = Security.SecurityAlgorithmSuite.Default _Binder.Security.Mode = BasicHttpSecurityMode.Transport System.Net.ServicePointManager.Expect100Continue = False End Sub 

Final class

Then call WS as follows:

 Dim binding As New MyBinding("some_name") 'Set endpoint address Dim address As EndpointAddress = _ New EndpointAddress("https://webservice-url?WSDL") 'Create web service client Dim ws As wsclient = New wsclient(binding._Binder, address) 

I am not on my PC with Visual Studio, so if the error is in the code above, let me know and I will check.

0
source

Source: https://habr.com/ru/post/1337364/


All Articles