BlackTDN Search

sábado, 3 de março de 2012

BlackTDN :: Autenticando Via tSocketClient

Exemplo de Autenticação via tSocketClient

#INCLUDE "PROTHEUS.CH"

#DEFINE MSECONDS_WAIT 5000

Static __cCRLF    := CRLF

/*/
    Funcao: tSocketAuth
    Autor:    Marinaldo de Jesus
    Data:    03/03/2012
    Uso:    Exemplo de Uso da Classe tSocketClient
/*/
User Function tSocketAuth()

    Local nVarNameL            := SetVarNameLen( 20 )

    Local atSocketC            := {}

    Local lGetError            := .F.

    Local cGetError
    Local ctSocketSend
    Local ctSocketReceive

    Local ntSocketReset
    Local ntSocketConnected
    Local ntSocketSend
    Local ntSocketReceive

    //Instanciamos um objeto do tipo Socket Client
    Local otSocketC    := tSocketClient():New()
    //Obtemos os Metodos da Classe
    atSocketC    := ClassMethArray( otSocketC )
    lGetError    := ( aScan( atSocketC , { |aMeth| aMeth[1] == "GETERROR" } ) > 0 )

    BEGIN SEQUENCE

        //Tentamos efetuar a Conexao a teste.com.br aguardando n milisegundos
        ntSocketConnected    := otSocketC:Connect( 80 , "http://user:password@teste.com.br" , MSECONDS_WAIT )
        //Verificamos se a conexao foi efetuada com sucesso
        IF !( otSocketC:IsConnected() ) //ntSocketConnected == 0 OK
            IF ( lGetError )
                //cGetError := otSocketC:GetError()
                DEFAULT cGetError := "otSocketC:GetError()"
                ConOut( cGetError )
            EndIF   
            ConOut( "" , "tSocketClient" , "" , "Sem Resposta a requisicao" , "" )
            BREAK
        EndIF

        ctSocketSend := "GET"
        ctSocketSend += " "
        ctSocketSend += "http://user:password@teste.com.br"
        ctSocketSend += __cCRLF
        ctSocketSend += "HTTP/1.0"
        ctSocketSend += __cCRLF
        ctSocketSend += "From:"
        ctSocketSend += " "
        ctSocketSend += "tsocketclient@sample.com"
        ctSocketSend += __cCRLF
        ctSocketSend += "User-Agent: tSocketClient/1.0"
        ctSocketSend += __cCRLF
        ctSocketSend += __cCRLF

        //Enviamos uma Mensagem
        ntSocketSend := otSocketC:Send( ctSocketSend )
        //Se a mensagem foi totalmente enviada
        IF ( ntSocketSend == Len( ctSocketSend ) )
            //Tentamos Obter a Resposta aguardando por n milisegundos
            ntSocketReceive := otSocketC:Receive( @ctSocketReceive , MSECONDS_WAIT )
            //Se Obtive alguma Resposta
            IF ( ntSocketReceive > 0 )
                //Direcionamo-a para o Console do Server
                ConOut( "" , ctSocketReceive , "" )
            Else
                IF ( lGetError )
                    //cGetError := otSocketC:GetError()
                    DEFAULT cGetError := "otSocketC:GetError()"
                    ConOut( cGetError )
                EndIF   
                ConOut( "" , "tSocketClient" , "" , "Sem Resposta a requisicao" , "" )
            EndIF
        Else
            IF ( lGetError )
                //cGetError := otSocketC:GetError()
                DEFAULT cGetError := "otSocketC:GetError()"
                ConOut( cGetError )
            EndIF   
            ConOut( "" , "tSocketClient" , "" , "Problemas no Enviamos da Mensagem" , "" )
        EndIF
        //Verificamos se ainda esta Conectado
        IF !( otSocketC:IsConnected() )
            //Tentamos Nova Conexao
            ntSocketReset         := otSocketC:ReSet() //ntSocketReset == 0 OK
            ntSocketConnected    := otSocketC:Connect( 80 , "http://user:password@teste.com.br" , MSECONDS_WAIT )       
        EndIF
        //Se permanecemos conectado ou reconectou
        IF !( otSocketC:IsConnected() ) //ntSocketConnected == 0 OK
            IF ( lGetError )
                //cGetError := otSocketC:GetError()
                DEFAULT cGetError := "otSocketC:GetError()"
                ConOut( cGetError )
            EndIF   
            ConOut( "" , "tSocketClient" , "" , "Sem Resposta a requisicao" , "" )
            BREAK
        EndIF

    END SEQUENCE

    //Encerramos a Conexao
    otSocketC:CloseConnection()
    otSocketC    := NIL

    SetVarNameLen( nVarNameL )

Return( ctSocketReceive )

[]s

иαldσ dj

Um comentário:

  1. Qual a necessidade de uso ? Quando tenho que abrir outro banco de dados, sempre terei que chamar esse socket ?

    ResponderExcluir