How to consume the WebService of the PrefeituraSP for NFS-e Error "client 'Anonymous'" VB.NET

2

I am developing a project that will send XMLs to the WS of the federal revenue, I made the reference using the following URL: link

But when you go through the Test Role EnvioRPS() you get the following error:    "The HTTP request is prohibited with the client authentication scheme 'Anonymous'"

To test WS I'm 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, ""))

Role searching for 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
    
asked by anonymous 03.03.2015 / 20:00

1 answer

1

Making a Custom Binding as follows, I was able to consume the WS and have 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
    
04.03.2015 / 14:25