Announcement

Collapse
No announcement yet.

Need help with TCP OPEN with UserID and Password

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with TCP OPEN with UserID and Password

    Need help with TCP OPEN with UserID and Password.

    The server evidently does not like the way I am handling the user id and password – see code below:

    Site$ = “www.MySite.com”
    File$ = http://UserID[email protected]/SECURE/index.html
    TCP OPEN "http" AT Site$ AS #1 TIMEOUT 60000
    TCP PRINT #1, "GET " & File$ & " HTTP/1.0"
    TCP PRINT #1, "Referer: http://www.MySite.com/"
    TCP PRINT #1, "User-Agent: User ID Test"
    TCP PRINT #1, ""


    When I execute the code above, I get the message below:

    Authorization Required
    This server could not verify that you
    are authorized to access the document
    requested. Either you supplied the wrong
    credentials (e.g., bad password), or your
    browser doesn’t understand how to supply
    the credentials required.

    I know the user id and password are correct - I'm testing on my own site.

    Does anyone have any suggestions?

    Thank you,
    Michael Rich

  • #2
    There should be a colon between the 'UserID' and the 'password' in the File$ string - not the smile icon.

    Sorry about that.

    Michael Rich

    Comment


    • #3
      To avoid the smiley, you need to tick the checkbox "Disable smilies in text" when composing the message.

      Even better, place your code in [ code] [ /code] tags (without the blank before code and /code)

      As for your problem, see Basic Access Authentification for how to send the credentials.

      Basically you need to encode the <user>:<password> part of the URL with BASE64 and send it along as a header line:

      Code:
       Site$ = “www.MySite.com”
      File$ = "http://www.MySite.com/SECURE/index.html"
      TCP OPEN "http" AT Site$ AS #1 TIMEOUT 60000
      TCP PRINT #1, "GET " & File$ & " HTTP/1.0"
      TCP PRINT #1, "Authorization: Basic <BASE64 encoded <user>:<password>>"
      Knuth
      Last edited by Knuth Konrad; 25 Jun 2008, 07:57 AM.

      Comment


      • #4
        Code:
        Function MimeEncode (ByVal sFileData As String) As String ' Encode data using Base64 encoding.
            Local  lBlock   As Long
            Local  lcBlocks As Long
            Local  lByte1   As Long
            Local  lByte2   As Long
            Local  pInput   As Byte Ptr
            Local  pOutput  As Byte Ptr
            Local  pTable   As Byte Ptr
            Static sBase64  As String
            Local  sResult  As String
            Local  Pad      As String
         
            If Len(sBase64)= 0 Then sBase64= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
         
            ' Calculate padding for Base64 stream
            Pad = String$(2 - (Len(sFileData) - 1) Mod 3, "=")
         
            ' Round up the length of the input data to a multiple of three
            lcBlocks = (Len(sFileData) + 2) \ 3
            If lcBlocks * 3 > Len(sFileData) Then
                sFileData = LSet$(sFileData, lcBlocks * 3 Using $Nul)
            End If
         
            ' Allocate the space for the output string
            sResult = Space$(lcBlocks * 4)
         
            ' Set up pointers so we can treat the data as byte streams
            pInput  = StrPtr(sFileData)
            pOutput = StrPtr(sResult)
            pTable  = StrPtr(sBase64)
         
            ' Loop through our entire input buffer
            For lBlock = 1 To lcBlocks
                ' Use the Base64 table to encode the output string
                lByte1 = @pInput
                Incr pInput
                lByte2 = @pInput
                @pOutput = @pTable[lByte1 \ 4]
                Incr pOutput
                @pOutput = @pTable[(lByte1 And 3) * 16 + lByte2 \ 16]
                Incr pOutput
                Incr pInput
                lByte1 = @pInput
                Incr pInput
                @pOutput = @pTable[(lByte2 And 15) * 4 + lByte1 \ 64]
                Incr pOutput
                @pOutput = @pTable[lByte1 And 63]
                Incr pOutput
            Next
         
            ' Merge in the padding bytes
            RSet Abs sResult = Pad
            Function = sResult
        End Function
        sigpic
        Mobile Solutions
        Sys Analyst and Development

        Comment


        • #5
          That's such a nicely written function, with a clean call interface.

          Why not post it in the Source Code Forum with a meaningful (i.e, "searchable") title, like "MIME Encode a String Base 64?"

          When I look for code, I look in Source Code Forum by subject. At least that is where and how I look first.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            You might want to use one of the many Windows APIs or COM objects available to do this much more easily, with full support for the https protocol. Here's one example:

            --pdf

            Comment


            • #7
              Actually, I think I did post this in the Source Code forum at one time...might have been in another thread that does it with Assembly. I just posted it here for those that search for titles like this or text like HTTP protocols as I would be searching for...and of course for the poster to use since I use it for this very purpose as I don't think I converted the code to do multiline in this, so this use is the main purpose.
              sigpic
              Mobile Solutions
              Sys Analyst and Development

              Comment


              • #8
                >Actually, I think I did post this in the Source Code forum

                Search, Forum=Source code, "subject only," all dates:
                "mime" ==> 3 posts not including above
                "encode" ==> 1 post not above

                Maybe you could post above as a "reply" to one of these so all like code is together?
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment

                Working...
                X