Solved

Download File with ServerXMLHTTP with setProxyCredentials

Posted on 2011-03-15
3
2,505 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

757 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

19 Experts available now in Live!

Get 1:1 Help Now