Consulting

Results 1 to 8 of 8

Thread: WINHTTP Request Returns NOT FOUND and UNAUTHORIZED ACCESS (VBA EXCEL)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    5
    Location

    WINHTTP Request Returns NOT FOUND and UNAUTHORIZED ACCESS (VBA EXCEL)

    Greetings:

    I am seeking a stable way to retrieve text files from a password protected web server. This had been working before but now it fails all my tests. I have confirmed that I can reach the file via a browser after manually entering the .htaccess credentials. I have tried the code below with different directories and with different file names, all to no avail. Perhaps another set of eyes. AND, if any one has a better idea on how to fetch these files, PLEASE let me know. I am at the end of my rope - help would be appreciated

    Thanks in advance:

    Public Function fetchMRTSF() As Boolean
    'Check WHTTP StatusText 
    '* THIS SUB LOADS THE PIA ORDER MRT SOURCE FILE FROM THE SPECIFIED WEB 
    '* SITE TO THE SPECIFIED LOCAL CLIENT DRIVE. THE PIA ORDER MRT SOURCE
    '* FILE CONTAINS ALL OF THE INFORMATION RELATIVE TO THE ASSOCIATE ORDER
    '* AND IS PARSED AND STORED USING THIS UTILITY.
    '* 1 SEPTEMBER 2016
    'Check WHTTP StatusText 
    'Dim _
    FileNum As Long, _
    FileData() As Byte, _
    WHTTP As Object, _
    TempStatus As Variant, _
    MainURL As String, _
    FileURL As String, _
    FilePath As String, _
    MyUser As String, _
    MyPass As String
    'Set URLs and Paths
    MainURL = ThisWorkbook.Sheets("CONTROL").Range("G1")
    FileURL = ThisWorkbook.Sheets("CONTROL").Range("D1") & _
    ThisWorkbook.Sheets("CONTROL").Range("D2")
    FilePath = ThisWorkbook.Sheets("CONTROL").Range("C3") & _
    ThisWorkbook.Sheets("CONTROL").Range("C2")
    'END
    'Set Server Credentials
    MyUser = Trim(ThisWorkbook.Sheets("CONTROL").Range("C5"))
    MyPass = Trim(ThisWorkbook.Sheets("CONTROL").Range("C6"))
    'END
    'Determine and Set WHTTP Version
    On Error Resume Next
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
    If Err.Number <> 0 Then
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    End If
    On Error GoTo 0
    'END
    'Post and Get WHTTP Commands and Data 
    'POST authentication string to the main website URL
    WHTTP.Open "POST", MainURL, False
    WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    'GET direct file url
    WHTTP.Open "GET", FileURL, False
    WHTTP.SetCredentials MyUser, MyPass, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
    WHTTP.Send
    ' Clear last stored WHTTP.StatusText
    ThisWorkbook.Sheets("CONTROL").Range("C7") = ""
    ' Gets Status Code
    StatusResults = WHTTP.StatusText
    ' Store WHTTP.StatusText
    ThisWorkbook.Sheets("CONTROL").Range("C7") = StatusResults
    'END
    'Check WHTTP StatusText
    If StatusResults = "OK" Then GoTo validFile
    'Path, File or Authinication Exception(Bad Request)
    MsgBox _
    "PIA Order MRT Source File Parser encountered the following exception." & _
    vbCrLf & "WHTTP Status Code: '" & StatusResults & "'" & vbCrLf & vbCrLf & _
    "Check to make certain that the URL path and MRT filename " & vbCrLf & _
    "are correct. Further, verify that the Username and Password " & vbCrLf & _
    "are accurate." & vbCrLf & vbCrLf & "NO MRT SOURCE FILE HAS BEEN DOWNLOADED!" _
    , vbCritical, "Fetch PIA Order MRT Source File Exception"
    fetchMRTSF = False
    Set WHTTP = Nothing
    Exit Function
    'END
    validFile:
    'Valid File Handler and Save Data Locally
    FileData = WHTTP.ResponseBody ' Gets file results
    FileNum = FreeFile ' Get next available file number
    Open FilePath For Binary Access Write As #FileNum
    Put #FileNum, 1, FileData
    Close #FileNum
    fetchMRTSF = True
    MsgBox _
    "PIA Order MRT source file has been successfully saved.", _
    vbInformation, "PIA Order MRT Source File Saved"
    'END
    End Function
    Last edited by Aussiebear; 09-03-2016 at 05:44 PM. Reason: Added code tags and tidied up presentation

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •