有没有人成功地使用 .Net FTPWebRequest 类从 NAT 后面 ftps 到远程服务器?我的代码如下:
Dim URI = "ftp://" & sRemoteDir & "/"
Dim ftp As FtpWebRequest = Nothing
Try
ServicePointManager.ServerCertificateValidationCallback = AddressOf ValidateServerCertificate
ftp = CType(FtpWebRequest.Create(URI), FtpWebRequest)
ftp.CachePolicy = New System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore)
ftp.Credentials = New NetworkCredential(sUID, sPw)
ftp.EnableSsl = True
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails
Dim response As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream
Dim streamReader As New StreamReader(responseStream)
Dim sb As New StringBuilder()
sb.Append(streamReader.ReadToEnd)
streamReader.Close()
response.Close()
Return sb.ToString()
Catch ex As Exception
Return (ex.Message)
End Try
我得到以下异常:
“无法从传输连接中读取数据:连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应。”
我知道代码很好,因为如果机器不在 NAT 后面,它就可以工作。有任何想法吗?这台机器有一个静态 IP 并且所有端口都对进出指定主机的流量开放。