FTP Download Translation VB6 to VB2005 - Needs a proxy?
Translating a subroutine from VB6 to VB2005. It downloads a single text file via FTP from an internal company server. Ive use this code in VB6 for many years and have not had to provide proxy credentials to get past the corporate firewall, only the credentials to get into the FTP server.
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
hconnection = InternetConnect(hOpen, HostURL, INTERNET_INVALID_PORT_NUMBER, user, PW, INTERNET_SERVICE_FTP, nFlag, 0)
DownloadFile = FtpGetFile(hconnection, File$, TargetPathFile$, False, INTERNET_FLAG_RELOAD, FTP_TRANSFER_TYPE_ASCII, 0)
In VB2005 I use
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Dim outPathAndFile As String = My.Computer.FileSystem.CombinePath(TargetPath, HostFileName)
Dim outputStream As New FileStream(outPathAndFile, FileMode.Create)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + HostURL + "/" + HostFileName)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = False 'True if binary file, False if ASCII text file
reqFTP.Credentials = New NetworkCreden开发者_开发技巧tial(id, pw)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
It errors out on the reqFTP.GetResponse with "The remote server returned an error. (407) Proxy authentication required". I just cant figure out why in my old code I never had to provide a proxy server and in the new code it seems like it requires it. I tried a manual connection via WS_FTP and it does not require a proxy so I must be doing something wrong but cant figure out what.
Ok in the interim I did some more research and found two things. One if I added that I need no proxy as follows reqFTP.Proxy = Nothing 'no proxy to get out of the corporate firewall since the server is internal that resolves my proxy problem. Once the proxy was resolved I had another issue with downloading from a mainframe which in the end my path and dataset name has to be in the form [code]ftpSite//'test.file2'[\code]. Once I did those two things it works like a charm.
精彩评论