How to consume Mayor’s Webservices for NFS-e Error "'Anonymous' client" VB.NET

Asked

Viewed 703 times

2

I am developing a project that will send Xmls to the WS of the IRS, I made the reference using the following URL: https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx

But when it passes the Test Function EnvioRPS() of the following error: "The HTTP request is prohibited with the client authentication scheme 'Anonymous'"

To make a test with WS I am doing the following, in a Button_click Event :

    Dim WS As New WebServiceNFSE_TESTE.LoteNFeSoapClient

    Dim Busca As X509Certificate2 = Buscar_Certificado_Nome("")

    'Teste
    MessageBox.Show(WS.EnvioRPS(1, ""))

Function seeking the certificate:

 Public Shared Function Buscar_Certificado_Nome(_nm_certificado As String) As X509Certificate2
    Dim X509_certificado As New X509Certificate2()
    Dim X509_store As New X509Store("MY", StoreLocation.CurrentUser)
    X509_store.Open(OpenFlags.[ReadOnly] Or OpenFlags.OpenExistingOnly Or OpenFlags.IncludeArchived)
    Dim X509_collection0 As X509Certificate2Collection = DirectCast(X509_store.Certificates, X509Certificate2Collection)
    Dim X509_collection1 As X509Certificate2Collection = DirectCast(X509_collection0.Find(X509FindType.FindByTimeValid, DateTime.Now, False), X509Certificate2Collection)
    Dim X509_collection2 As X509Certificate2Collection = DirectCast(X509_collection0.Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.DigitalSignature, False), X509Certificate2Collection)

    If _nm_certificado = "" Then
        Dim scollection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(X509_collection2, "Certificado(s) Digital(is) disponível(is)", "Selecione o Certificado Digital para uso no aplicativo", X509SelectionFlag.SingleSelection)
        If scollection.Count = 0 Then
            'Nenhum certificado escolhido
            X509_certificado.Reset()
        Else
            X509_certificado = scollection(0)
        End If
    Else
        Dim scollection As X509Certificate2Collection = X509_collection1.Find(X509FindType.FindByThumbprint, "ce c9 ed 7a b6 54 fe fe 1b bf 31 78 ef 5f 74 be be 6e e8 12", True)
        If scollection.Count = 0 Then
            'Nenhum certificado válido foi encontrado com o nome informado
            X509_certificado.Reset()
        Else
            X509_certificado = scollection(0)
        End If
    End If
    X509_store.Close()
    Return X509_certificado
End Function

1 answer

1

Making a Custom Binding as follows, I was able to consume WS and get the return using the following code:

        Dim Certificado As X509Certificate2
    Dim WS As WebServiceNFSE_TESTE.LoteNFeSoapClient

    Dim httpstransport As New ServiceModel.Channels.HttpsTransportBindingElement

    Try
        Certificado = Buscar_Certificado_Nome("")
        httpstransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard
        httpstransport.ManualAddressing = False
        httpstransport.MaxBufferPoolSize = 524288
        httpstransport.MaxReceivedMessageSize = 65536
        httpstransport.AllowCookies = False
        httpstransport.BypassProxyOnLocal = False
        httpstransport.KeepAliveEnabled = True
        httpstransport.MaxBufferSize = 65536
        httpstransport.TransferMode = TransferMode.Buffered
        httpstransport.UnsafeConnectionNtlmAuthentication = True
        httpstransport.UseDefaultWebProxy = True
        httpstransport.RequireClientCertificate = True

        Dim MessageEncoding As New TextMessageEncodingBindingElement()
        MessageEncoding.MessageVersion = MessageVersion.Soap12
        MessageEncoding.WriteEncoding = Encoding.UTF8
        MessageEncoding.MaxReadPoolSize = 64
        MessageEncoding.MaxWritePoolSize = 16

        Dim CustomBinding As New ServiceModel.Channels.CustomBinding(MessageEncoding, httpstransport)

        Dim uri As String() = {"https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx"}
        Dim EndpointAddress As New ServiceModel.EndpointAddress(uri(0))

         WS = New WebServiceNFSE_TESTE.LoteNFeSoapClient(CustomBinding, EndpointAddress)
         WS.ClientCredentials.ClientCertificate.Certificate = Certificado

        Dim oXml As New XmlDocument
        oXml.Load("C:\PedidoEnvioRPS.xml")

         MsgBox(WS.EnvioRPS(1, oXml.InnerText))

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

Browser other questions tagged

You are not signed in. Login or sign up in order to post.