Solved

Download File with ServerXMLHTTP with setProxyCredentials

Posted on 2011-03-15
3
2,559 Views
Last Modified: 2012-05-11
I am trying to run a VBScrpit to download a file daily from a website. In order to do this I need to set the proxy credentials. THe scrpit below works inside the network, and I have verified that the two URLs are accurate (if put in IE both go where I want) but after I enter my proxy credentials and do a .send the object status returns HTTP Error 407 (proxy authorization required) I know that the credentials I have are what I enter manually, although I reliase that it may not be the full username. Also I cant bypass the proxy because the website verifies the IP address before allowing me to login.  Any suggestions?  
Dim sFileURL, objXMLHTTP, objADOStream, objFSO, sLocation,  username, password,url
     
 username = "my username"
 password = "my password"
 sFileURL = "Login URL"
 sLocation = "Path" 
 url = "File URL"
 
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")

 objXMLHTTP.setProxy 2, "my proxy"   
 objXMLHTTP.Open "GET", sFileURL, False 
 objXMLHTTP.setProxyCredentials username, password
 objXMLHTTP.send()
 
If objXMLHTTP.Status <> 200 Then    
	msgbox objXMLHTTP.status 
End if 
 
objXMLHTTP.Open "GET", url, False
objXMLHTTP.send()

If objXMLHTTP.Status = 200 Then
 
        Set objADOStream = CreateObject("ADODB.Stream")
        objADOStream.Open
 
        objADOStream.Type = 1
        objADOStream.Write objXMLHTTP.ResponseBody
 
        objADOStream.Position = 0
 
        Set objFSO = CreateObject
                     ("Scripting.FileSystemObject")
 
         If objFSO.FileExists(sLocation) Then 
                      objFSO.DeleteFile sLocation
 
        
         Set objFSO = Nothing
 
        
         objADOStream.SaveToFile sLocation
 
         objADOStream.Close
  
         Set objADOStream = Nothing
 
End If
 
Set objXMLHTTP = Nothing

Open in new window

0
Comment
Question by:JClayton7
  • 2
3 Comments
 

Author Comment

by:JClayton7
ID: 35139730
I got it to work, It was a username issue. But the file i am trying to download is a .gz and all I seem to be getting is the XML not the actual file. Any help would be greatly appreciated!
0
 
LVL 6

Accepted Solution

by:
judgeking earned 250 total points
ID: 35140297
This uses different code objects, but it works for most downloads.  Don't forget, in the server IIS settings, it must be set to serve the type of file you're trying to download, or else you'll receive an error.


Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Private Sub Form_Load()
    Dim ret As Long
    Dim buff As String
    
    Call DeleteUrlCacheEntry("http://craphound.com/msftdrm.txt")
    ret = URLDownloadToFile(0, "http://craphound.com/msftdrm.txt", "c:\test.txt", 0, 0)
    If ret = 0 Then
        Open "c:\test.txt" For Binary As 1
            buff = Space$(LOF(1))
            Get #1, 1, buff
        Close 1
    End If
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:judgeking
ID: 35402817
Clayton, any update?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now