跪求VB.NET 连接FTP

跪求VB.NET 连接FTP 怎么都不行 即将崩溃 用INET 和 API的InternetOpen

InternetOpen 总是返回空 就是 失败

请问谁有方法 在线等

或者 只要能连接FTP 测试账号密码正确与否就成

Imports System.Net
Imports System.IO

Module FtpSample

Sub Main(ByVal args() As String)
If args.Length = 0 OrElse args(0).Equals("/?") Then
DisplayUsage()
ElseIf args.Length = 1 Then
Download(args(0))
ElseIf args.Length = 2 Then
If args(0).Equals("/list") Then
List(args(1))
Else
Upload(args(0), args(1))
End If
Else
Console.WriteLine("Unrecognized argument.")
End If
End Sub

Private Sub DisplayUsage()
Console.WriteLine("USAGE:")
Console.WriteLine(" FtpSample [/? | <FTP download URL> | <local file>")
Console.WriteLine(" <FTP upload URL> | /list <FTP list URL>]")
Console.WriteLine()
Console.WriteLine("where")
Console.WriteLine(" FTP download URL URL of a file to download from an FTP server.")
Console.WriteLine(" FTP upload URL Location on a FTP server to upload a file to.")
Console.WriteLine(" FTP list URL Location on a FTP server to list the contents of.")
Console.WriteLine(" local file A local file to upload to an FTP server.")
Console.WriteLine()
Console.WriteLine(" Options:")
Console.WriteLine(" /? Display this help message.")
Console.WriteLine(" /list Specifies the list command.")
Console.WriteLine()
Console.WriteLine("EXAMPLES:")
Console.WriteLine(" Download a file FtpSample ftp://myserver/download.txt")
Console.WriteLine(" Upload a file FtpSample upload.txt ftp://myserver/upload.txt")
End Sub

Private Sub Download(ByVal downloadUrl As String)
Dim responseStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim reader As StreamReader = Nothing
Try
Dim downloadRequest As FtpWebRequest = _
WebRequest.Create(downloadUrl)
Dim downloadResponse As FtpWebResponse = _
downloadRequest.GetResponse()
responseStream = downloadResponse.GetResponseStream()

Dim fileName As String = _
Path.GetFileName(downloadRequest.RequestUri.AbsolutePath)

If fileName.Length = 0 Then
reader = New StreamReader(responseStream)
Console.WriteLine(reader.ReadToEnd())
Else
fileStream = File.Create(fileName)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = responseStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
fileStream.Write(buffer, 0, bytesRead)
End While
End If
Console.WriteLine("Download complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
ElseIf responseStream IsNot Nothing Then
responseStream.Close()
End If
If fileStream IsNot Nothing Then
fileStream.Close()
End If
End Try
End Sub

Private Sub Upload(ByVal fileName As String, ByVal uploadUrl As String)
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Try
Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile

' UploadFile is not supported through an Http proxy
' so we disable the proxy for this request.
uploadRequest.Proxy = Nothing

requestStream = uploadRequest.GetRequestStream()
fileStream = File.Open(fileName, FileMode.Open)

Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
requestStream.Write(buffer, 0, bytesRead)
End While

' The request stream must be closed before getting the response.
requestStream.Close()

uploadResponse = uploadRequest.GetResponse()
Console.WriteLine("Upload complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Finally
If uploadResponse IsNot Nothing Then
uploadResponse.Close()
End If
If fileStream IsNot Nothing Then
fileStream.Close()
End If
If requestStream IsNot Nothing Then
requestStream.Close()
End If
End Try
End Sub

Private Sub List(ByVal listUrl As String)
Dim reader As StreamReader = Nothing
Try
Dim listRequest As FtpWebRequest = WebRequest.Create(listUrl)
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
Dim listResponse As FtpWebResponse = listRequest.GetResponse()
reader = New StreamReader(listResponse.GetResponseStream())
Console.WriteLine(reader.ReadToEnd())
Console.WriteLine("List complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
End If
End Try
End Sub

End Module

可以通过设置 Credentials 属性来指定用于连接服务器的凭据,也可以将它们包含在传递给 Create 方法的 URI 的 UserInfo 部分中。

从 FTP 服务器下载文件时,如果命令成功,所请求的文件的内容即在响应对象的流中。通过调用 GetResponseStream 方法,可以访问此流。

如果使用 FtpWebRequest 对象向服务器上载文件,则必须将文件内容写入请求流,请求流是通过调用 GetRequestStream 方法或其异步对应方法(BeginGetRequestStream 和 EndGetRequestStream 方法)获取的。必须写入流并在发送请求之前关闭该流。

请求是通过调用 GetResponse 方法或其异步对应方法(BeginGetResponse 和 EndGetResponse 方法)发送到服务器的。请求的操作完成时,会返回一个 FtpWebResponse 对象。FtpWebResponse 对象提供操作的状态以及从服务器下载的所有数据。

参考资料:msdn

温馨提示:内容为网友见解,仅供参考
第1个回答  2010-06-26
先用 My.Computer.Network.Ping(IP_Addr)看IP地址通不通,再用My.Computer.Network.DownloadFile(URLPath, SavePath, UserName, PassWord)

我有个FTP定时取文件就用的这个,很好用噢。
URLPath="FTP://" & IP & "/" & FileName

---------------------------
使用.net后,尽量少用API,知道.NET为什么叫.NET么?就是网络功能很多都有现成的封装好的啦。^_^本回答被提问者采纳

跪求VB.NET 连接FTP
Console.WriteLine(" FTP download URL URL of a file to download from an FTP server.")Console.WriteLine(" FTP upload URL Location on a FTP server to upload a file to.")Console.WriteLine(" FTP list URL Location on a FTP server to list the contents of.")Cons...

ftp上传文件用vb.net怎么实现
My.Computer.Network.UploadFile(本地文件路径, ftp服务器路径包括文件名,用户名,密码)

VB.net连接FTP操作
Console.WriteLine(" FTP download URL URL of a file to download from an FTP server.")Console.WriteLine(" FTP upload URL Location on a FTP server to upload a file to.")Console.WriteLine(" FTP list URL Location on a FTP server to list the contents of.")Cons...

VBNET从FTP下载文件,需要先判断文件是否存在吗
需要。在实际使用FTP文件服务器的过程中,经常需要远程下载解析文件。为提高效率,需要判断文件存在与否,有选择的进行解析。FTP协议是一个用于在计算机网络上客户端和服务器之间进行文件传输的应用层协议,包括FTP服务器和FTP客户端两个组成部分。FTP能操作任何类型的文件而不需要进一步处理,但有着极高的延...

vb如何上传文件到指定ftp?
最简单的方法是用ftp批处理,代码如下open app.path & "\\ftpcmd.ftp" for output as #1 '创建一个ftp命令文件print #1, "open " & "IP" '打开ftp服务器print #1, "user " & "用户名" print #1, "pass " & "密码"print #1, "lcd " & "本地文件路径" '指定本地文件所在目录print #1, "put ...

网页ftp怎么上传文件网页ftp怎么上传文件夹
1.如果在服务器上设置FTP,比如在IIS中,只需要访问绑定的域名;2.如果你使用的是第三方软件制作的FTP,比如Serv-U,那么你需要获取IP地址,比如192.168.1.1,然后你可以在地址栏输入。Ftp:\/\/192.168.1.1,弹出一个对话框,输入用户名和密码即可!如果服务器设置了匿名访问,那么就不需要用户名和...

求vb.net入门经典(第三版)电子书
ftp:\/\/csdn:csdn@61.132.59.166\/VB.NET 入门经典 (第三版).rar

我想用vb.net写个在线升级程序,上网找个代码,里面AxWebBrowser是什么...
AxWebBowser就是原来VB6里面那个COM控件WebBowser.直接在添加引用时选择COM一页进去找就是了 在线升级一般采用FTP方式.先由客户端发起更新请求,服务器返回现在最新的文件的清单、版本、修改日期等信息,客户端拿到和自己的文件进行比对,发现不同的就用FTP方式向服务器索取最新的,然后替换掉自己的就可以了...

vb.net怎么在非局域网的两个电脑之间互传文字信息?
如果你家里是公网ip或者公网IP能映射内网IP的话可以直接搭一个服务器,否则只能使用内网穿透软件然后再搭建服务器。如应用服务器、网页服务器、电子邮件服务、 FTP 服务器等等都可以进行消息交流。

VB6.0 FTP下载上传问题【可加分!】
首先介绍ftp下载,代码如下: Const Str_FtpServer As String = " ftp:\/\/ftp.lob.cn" Private Str_Username As String = "lob" Private Str_Password As String = "xxxxxxxx" Private Sub DownloadFile(ByVal Str_Path As String) Try Dim Str_Filename As String = Str_Path Dim ftpReq As ...

相似回答
大家正在搜