关于winform程序使用代理上网时无法正常访问FTP的问题!

一只熊猫 2009-04-08 11:22:35
C/S程序,用来上传、下载文档。

当客户端使用代理上网连接FTP出现如下提示:

The requested ftp command is not supported when using HTTP proxy

FTP服务器是用IIS6.0搭建的,使用的是默认的端口21,禁止了匿名访问。

据客户端那边的技术员说,他们所有通过IE上网都走代理服务器,其他程序不走代理,而且也没有屏蔽常用端口。

情况如上,请大家指教,该如何处理?知识面不够广想写个让客户觉得好用的程序真费力啊 =。=
...全文
566 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ldd10345 2010-07-21
  • 打赏
  • 举报
回复
VB.NET code Public Class FTPreq Public progess As Integer = 0 '传输进度 Public Cancel As Boolean = False '是否放弃 Public Done As Boolean = False '是否完成 Public Backmsg As String = "" '出错信息 Public filePath As String = "" '下载保存位置 Public fileName As String = "" '目标文件 Public ftpServerIp As String = "" '服务器 Public ftpUserID As String = "" '帐号 Public ftpPassword As String = "" '密码 Public WorkDir As String = "" '工作目录 Public dirName As String = "" '目标目录 Public Err As Boolean = False '是否出错 Public Wait As Boolean = False '是否等待 Public Sub Download() Dim filesize As Long = GetFileSize() '使用http代理时无法通过getfilesize获得文件大小 Dim CosFlag As Boolean = False If filesize = -1 Then filesize = GetSize() : CosFlag = True '使用ListDirectoryDetails获取文件夹列表,再从中截取文件大小 If filesize = -1 Then Err = True Backmsg = "无法获得目标文件大小,放弃下载。" End If Cancel = False Dim reqFTP As FtpWebRequest Dim consfile As String = "" If CosFlag Then consfile = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("gb2312")) Else consfile = fileName End If Try reqFTP = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir & "/" & consfile)), FtpWebRequest) reqFTP.Method = WebRequestMethods.Ftp.DownloadFile reqFTP.UseBinary = True reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword) Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse) Dim ftpStream As Stream = 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) {} Dim counti As Integer = 0 readCount = ftpStream.Read(buffer, 0, bufferSize) progess = 0 Dim outputStream As New FileStream((filePath & "\") + fileName, FileMode.Create) Try While readCount > 0 If Cancel Then progess = 0 Exit While End If If Wait Then My.Application.DoEvents() Continue While End If outputStream.Write(buffer, 0, readCount) readCount = ftpStream.Read(buffer, 0, bufferSize) counti = counti + readCount If counti >= filesize Then progess = 100 Else progess = Int(counti / filesize * 100) End While progess = 100 Done = True Catch ex As Exception Backmsg = ex.Message Err = True End Try ftpStream.Close() outputStream.Close() response.Close() Catch ex As Exception Backmsg = ex.Message Err = True Return End Try End Sub Public Function GetFileSize() As Long Dim reqFTP As FtpWebRequest Dim fileSize As Long = 0 Try Dim fileInf As FileInfo = New FileInfo(fileName) reqFTP = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir & "/" & fileName)), FtpWebRequest) reqFTP.Method = WebRequestMethods.Ftp.GetFileSize reqFTP.UseBinary = True reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword) Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse) fileSize = response.ContentLength response.Close() Catch ex As Exception Backmsg = ex.Message 'Err = True Return -1 End Try Return fileSize End Function Public Function GetSize() As Long '使用http代理时用此函数获取文件大小 Dim listRequest As FtpWebRequest = Nothing Dim listResponse As FtpWebResponse = Nothing Dim responseStream As Stream = Nothing Dim readStream As StreamReader = Nothing Dim filesize As Integer = 0 Try listRequest = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir)), FtpWebRequest) listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails listRequest.Credentials = New NetworkCredential(ftpUserID, ftpPassword) listResponse = DirectCast(listRequest.GetResponse(), FtpWebResponse) responseStream = listResponse.GetResponseStream() readStream = New StreamReader(responseStream, System.Text.Encoding.[Default]) If readStream IsNot Nothing Then Dim tempstr As String = readStream.ReadToEnd Dim ed As Integer = tempstr.IndexOf("MB") '截取文件大小字符串,转换为实际大小 If ed = -1 Then ed = tempstr.IndexOf("KB") filesize = Val(tempstr.Substring(0, tempstr.IndexOf("KB"))) * 1024 Else tempstr = tempstr.Substring(ed - 2, 6).Trim filesize = Val(tempstr.Substring(0, tempstr.IndexOf("MB"))) * 1024 * 1024 End If Else Backmsg = "未能获得文件大小" filesize = -1 End If Catch ex As Exception Backmsg = ex.Message filesize = -1 End Try listResponse.Close() responseStream.Close() readStream.Close() Return filesize End Function End Class
ldd10345 2010-07-21
  • 打赏
  • 举报
回复
VB.NET code Public Class FTPreq Public progess As Integer = 0 '传输进度 Public Cancel As Boolean = False '是否放弃 Public Done As Boolean = False '是否完成 Public Backmsg As String = "" '出错信息 Public filePath As String = "" '下载保存位置 Public fileName As String = "" '目标文件 Public ftpServerIp As String = "" '服务器 Public ftpUserID As String = "" '帐号 Public ftpPassword As String = "" '密码 Public WorkDir As String = "" '工作目录 Public dirName As String = "" '目标目录 Public Err As Boolean = False '是否出错 Public Wait As Boolean = False '是否等待 Public Sub Download() Dim filesize As Long = GetFileSize() '使用http代理时无法通过getfilesize获得文件大小 Dim CosFlag As Boolean = False If filesize = -1 Then filesize = GetSize() : CosFlag = True '使用ListDirectoryDetails获取文件夹列表,再从中截取文件大小 If filesize = -1 Then Err = True Backmsg = "无法获得目标文件大小,放弃下载。" End If Cancel = False Dim reqFTP As FtpWebRequest Dim consfile As String = "" If CosFlag Then consfile = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("gb2312")) Else consfile = fileName End If Try reqFTP = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir & "/" & consfile)), FtpWebRequest) reqFTP.Method = WebRequestMethods.Ftp.DownloadFile reqFTP.UseBinary = True reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword) Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse) Dim ftpStream As Stream = 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) {} Dim counti As Integer = 0 readCount = ftpStream.Read(buffer, 0, bufferSize) progess = 0 Dim outputStream As New FileStream((filePath & "\") + fileName, FileMode.Create) Try While readCount > 0 If Cancel Then progess = 0 Exit While End If If Wait Then My.Application.DoEvents() Continue While End If outputStream.Write(buffer, 0, readCount) readCount = ftpStream.Read(buffer, 0, bufferSize) counti = counti + readCount If counti >= filesize Then progess = 100 Else progess = Int(counti / filesize * 100) End While progess = 100 Done = True Catch ex As Exception Backmsg = ex.Message Err = True End Try ftpStream.Close() outputStream.Close() response.Close() Catch ex As Exception Backmsg = ex.Message Err = True Return End Try End Sub Public Function GetFileSize() As Long Dim reqFTP As FtpWebRequest Dim fileSize As Long = 0 Try Dim fileInf As FileInfo = New FileInfo(fileName) reqFTP = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir & "/" & fileName)), FtpWebRequest) reqFTP.Method = WebRequestMethods.Ftp.GetFileSize reqFTP.UseBinary = True reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword) Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse) fileSize = response.ContentLength response.Close() Catch ex As Exception Backmsg = ex.Message 'Err = True Return -1 End Try Return fileSize End Function Public Function GetSize() As Long '使用http代理时用此函数获取文件大小 Dim listRequest As FtpWebRequest = Nothing Dim listResponse As FtpWebResponse = Nothing Dim responseStream As Stream = Nothing Dim readStream As StreamReader = Nothing Dim filesize As Integer = 0 Try listRequest = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir)), FtpWebRequest) listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails listRequest.Credentials = New NetworkCredential(ftpUserID, ftpPassword) listResponse = DirectCast(listRequest.GetResponse(), FtpWebResponse) responseStream = listResponse.GetResponseStream() readStream = New StreamReader(responseStream, System.Text.Encoding.[Default]) If readStream IsNot Nothing Then Dim tempstr As String = readStream.ReadToEnd Dim ed As Integer = tempstr.IndexOf("MB") '截取文件大小字符串,转换为实际大小 If ed = -1 Then ed = tempstr.IndexOf("KB") filesize = Val(tempstr.Substring(0, tempstr.IndexOf("KB"))) * 1024 Else tempstr = tempstr.Substring(ed - 2, 6).Trim filesize = Val(tempstr.Substring(0, tempstr.IndexOf("MB"))) * 1024 * 1024 End If Else Backmsg = "未能获得文件大小" filesize = -1 End If Catch ex As Exception Backmsg = ex.Message filesize = -1 End Try listResponse.Close() responseStream.Close() readStream.Close() Return filesize End Function End Class
一只熊猫 2009-04-11
  • 打赏
  • 举报
回复
终于解决了,我把我的方法贴出来。希望大家解决问题后也能把方法贴出来。


Public Class FTPreq

Public progess As Integer = 0 '传输进度
Public Cancel As Boolean = False '是否放弃
Public Done As Boolean = False '是否完成
Public Backmsg As String = "" '出错信息
Public filePath As String = "" '下载保存位置
Public fileName As String = "" '目标文件
Public ftpServerIp As String = "" '服务器
Public ftpUserID As String = "" '帐号
Public ftpPassword As String = "" '密码
Public WorkDir As String = "" '工作目录
Public dirName As String = "" '目标目录
Public Err As Boolean = False '是否出错
Public Wait As Boolean = False '是否等待

Public Sub Download()
Dim filesize As Long = GetFileSize() '使用http代理时无法通过getfilesize获得文件大小
Dim CosFlag As Boolean = False
If filesize = -1 Then filesize = GetSize() : CosFlag = True '使用ListDirectoryDetails获取文件夹列表,再从中截取文件大小
If filesize = -1 Then
Err = True
Backmsg = "无法获得目标文件大小,放弃下载。"
End If
Cancel = False
Dim reqFTP As FtpWebRequest
Dim consfile As String = ""
If CosFlag Then
consfile = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("gb2312"))
Else
consfile = fileName
End If
Try
reqFTP = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir & "/" & consfile)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
Dim ftpStream As Stream = 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) {}
Dim counti As Integer = 0
readCount = ftpStream.Read(buffer, 0, bufferSize)
progess = 0
Dim outputStream As New FileStream((filePath & "\") + fileName, FileMode.Create)
Try
While readCount > 0
If Cancel Then
progess = 0
Exit While
End If
If Wait Then
My.Application.DoEvents()
Continue While
End If
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
counti = counti + readCount
If counti >= filesize Then progess = 100 Else progess = Int(counti / filesize * 100)
End While
progess = 100
Done = True
Catch ex As Exception
Backmsg = ex.Message
Err = True
End Try
ftpStream.Close()
outputStream.Close()
response.Close()
Catch ex As Exception
Backmsg = ex.Message
Err = True
Return
End Try
End Sub

Public Function GetFileSize() As Long
Dim reqFTP As FtpWebRequest
Dim fileSize As Long = 0
Try
Dim fileInf As FileInfo = New FileInfo(fileName)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir & "/" & fileName)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
fileSize = response.ContentLength
response.Close()
Catch ex As Exception
Backmsg = ex.Message
'Err = True
Return -1
End Try
Return fileSize
End Function

Public Function GetSize() As Long '使用http代理时用此函数获取文件大小
Dim listRequest As FtpWebRequest = Nothing
Dim listResponse As FtpWebResponse = Nothing
Dim responseStream As Stream = Nothing
Dim readStream As StreamReader = Nothing
Dim filesize As Integer = 0
Try
listRequest = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & WorkDir)), FtpWebRequest)
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
listRequest.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
listResponse = DirectCast(listRequest.GetResponse(), FtpWebResponse)
responseStream = listResponse.GetResponseStream()
readStream = New StreamReader(responseStream, System.Text.Encoding.[Default])
If readStream IsNot Nothing Then
Dim tempstr As String = readStream.ReadToEnd
Dim ed As Integer = tempstr.IndexOf("MB") '截取文件大小字符串,转换为实际大小
If ed = -1 Then
ed = tempstr.IndexOf("KB")
filesize = Val(tempstr.Substring(0, tempstr.IndexOf("KB"))) * 1024
Else
tempstr = tempstr.Substring(ed - 2, 6).Trim
filesize = Val(tempstr.Substring(0, tempstr.IndexOf("MB"))) * 1024 * 1024
End If
Else
Backmsg = "未能获得文件大小"
filesize = -1
End If
Catch ex As Exception
Backmsg = ex.Message
filesize = -1
End Try
listResponse.Close()
responseStream.Close()
readStream.Close()
Return filesize
End Function
End Class
messi_yang 2009-04-10
  • 打赏
  • 举报
回复
幫頂了。這是我自己做的一個簡單例子:

Private Sub CreateFolder(ByVal FolderName As String)
Dim Str_FtpServer As String

Str_FtpServer = "ftp://192.168.1.48/test" ’自己設置的伺服器
Try

Dim Str_Folder As String = Str_FtpServer & "/" & FolderName
Dim ftpReq As Net.FtpWebRequest = Net.WebRequest.Create(Str_Folder)
ftpReq.Method = Net.WebRequestMethods.Ftp.MakeDirectory
ftpReq.Credentials = New Net.NetworkCredential("ftpuser", "ftppass")
Dim ftpResp As Net.FtpWebResponse = ftpReq.GetResponse
ftpResp.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub LoadDownFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadDownFile.Click
My.Computer.Network.DownloadFile("ftp://192.168.1.48/002.rpt", "001.rpt", "ftpuser", "ftppass", True, 100, True) '下载

End Sub
一只熊猫 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 messi_yang 的回复:]
幫頂了。這是我自己做的一個簡單例子:

Private Sub CreateFolder(ByVal FolderName As String)
Dim Str_FtpServer As String

Str_FtpServer = "ftp://192.168.1.48/test" ’自己設置的伺服器
Try

Dim Str_Folder As String = Str_FtpServer & "/" & FolderName
Dim ftpReq As Net.FtpWebRequest = Net.WebRequest.Create(Str_Folder)
ftpReq.Method = Net.WebRequestMethods.Ftp.MakeDirectory
ftpReq.Credential…
[/Quote]

没有使用代理啊!
LS_2006 2009-04-09
  • 打赏
  • 举报
回复
欢迎加入我们的QQ群,大家共同学习,共同提高!

程序员中的战斗机,QQ群号:84505938
一只熊猫 2009-04-09
  • 打赏
  • 举报
回复
没人知道啊
一只熊猫 2009-04-08
  • 打赏
  • 举报
回复
忘了说,c/s程序中是使用System.Net.FtpWebRequest实现上传下载的。
一只熊猫 2009-04-08
  • 打赏
  • 举报
回复
“如果设置了 Proxy 属性(直接设置或在配置文件中设置),与 FTP 服务器的通信将通过指定的代理进行。如果指定的代理是 HTTP 代理,则仅支持 DownloadFile、ListDirectory 和 ListDirectoryDetails 命令。”

这句话是什么意思呢?
wuyq11 2009-04-08
  • 打赏
  • 举报
回复
ftp不支持代理,只能使用其他方法
一只熊猫 2009-04-08
  • 打赏
  • 举报
回复
顶顶
一只熊猫 2009-04-08
  • 打赏
  • 举报
回复
用System.Net.FtpWebRequest.Porxy可以设置代理服务器。

MSDN上有这么一句话:如果设置了 Proxy 属性(直接设置或在配置文件中设置),与 FTP 服务器的通信将通过指定的代理进行。如果指定的代理是 HTTP 代理,则仅支持 DownloadFile、ListDirectory 和 ListDirectoryDetails 命令。

那么是不是下面的代码可以实现呢? 假设Http代理支持Getfilesize命令。。


Public Function GetFileSize(fileName) As Long
Dim reqFTP As FtpWebRequest
Dim fileSize As Long = 0
Try
Dim fileInf As FileInfo = New FileInfo(fileName)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIp & "/") & fileName)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize
reqFTP.Proxy.Credentials = New NetworkCredential("帐号", "密码", "地址") '是不是这样就可以了呢?
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
fileSize = response.ContentLength
response.Close()
Catch ex As Exception
Backmsg = ex.Message
Err = True
Return -1
End Try
Return fileSize



请问如何模拟Http代理上网的环境呢?





一只熊猫 2009-04-08
  • 打赏
  • 举报
回复
=。=

让客户不用代理,这个基本没可能。
du9232 2009-04-08
  • 打赏
  • 举报
回复
呵呵,试过网上的方案,没有试通。最后还是让客户自己那边把代理去掉

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧