如何访问FTP(http://)服务器上的文件并使用其中的值(无下载) [英] how to access file on a FTP (http://) Server and use the values inside (no Download)

查看:74
本文介绍了如何访问FTP(http://)服务器上的文件并使用其中的值(无下载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我尝试使用SQL表在FTP服务器上查找我可以通过常规HTTP访问的文件。



我使用登录用户名来检索文件的正确名称。



我的问题是我现在不知道如何访问ftp。

我尝试了不同的方法和指南,但每个人都解释了如何下载或上传文件给用户,而不是如何访问它以使用我背后的代码中的内容。 (VB脚本)



sql接收部分有效。





 受保护的  Sub  Page_Load(发件人作为 对象,e  As  System.EventArgs)句柄  .Load 

Dim myuser As MembershipUser
myuser = Membership.GetUser(User.Identity.Name)
Dim myuserString As String
myuserString = myuser.UserName

Dim strQuery As String =( SELECT UserID,kFile FROM [Userinfo] WHERE [UserID] = @UserID
Dim con 作为 SqlConnection(ConfigurationManager.ConnectionStrings( UserinfoConnectionString)。ConnectionString)
Dim cmd As SqlCommand()
cmd.Parameters.AddWithValue( @ UserID,myuser.UserName)
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd .Connection = con

尝试
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
while sdr.Read()

UserIDLabel.Text = sdr( UserID)。ToString()
KioskFileLabel.Text = sdr( kFile)。ToString()

Const 服务器作为 字符串 = http://192.168.3.50:81/_0/
Dim 地址作为 字符串 =服务器+ sdr( kFile
Dim 文件 As StreamReader(添加ress,System.Text.Encoding.UTF8)

FTPFileLabel.Text = file.ReadToEnd()

file.Close()

结束

最后

con.Close()
con.Dispose()

结束 尝试
结束 Sub

解决方案

0)FTP或HTTP比?

1)StreamReader不使用任何协议处理程序。您不能使用它来读取URL / URI上的资源。

2)使用 WebRequest [ ^ ]而是改为。这是非常复杂的类,你可以将它用于ftp和http请求。

+1)你仍然下载。如果将检索到的内容保存到文件中,则http或ftp流量没有实际差异。


来自代码

  Const  server  As   String  =   http://192.168.3.50:81/_0/ 



我将代码更改为



  Dim  request < span class =code-keyword> As  WebRequest = WebRequest.Create(  http:// server / + sdr(  kFile))
Dim 响应 As WebResponse = request.GetResponse()

Console.WriteLine( CType (响应,HttpWebResp onse).StatusDescription)

Dim dataStream As Stream = response.GetResponseStream( )

Dim reader As StreamReader(dataStream)

Dim responseFromServer As String = reader.ReadToEnd()

Console.WriteLine(responseFromServer)



FTPFileLabel .Text = responseFromServer

reader.Close()
response.Close()
结束 while

最后

con.Close()
con.Dispose()


结束 尝试
结束 Sub





并感谢ZoltánZörgő的帮助。


Hi
I try to use a SQL Table to find a file on an FTP server that I can access via regular HTTP.

I use the login user name to retrieve the correct name of the file.

My problem is that I do not now how to access the ftp.
I have tried different methods and guides, but everyone explains how to download or upload the file to the user, and not how I can acces it to use the content inside my code behind. (VB script)

the sql receive part works.


Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        
        Dim myuser As MembershipUser
        myuser = Membership.GetUser(User.Identity.Name)
        Dim myuserString As String
        myuserString = myuser.UserName
                      
        Dim strQuery As String = ("SELECT UserID, kFile FROM [Userinfo] WHERE [UserID] = @UserID")
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("UserinfoConnectionString").ConnectionString)
        Dim cmd As New SqlCommand()
        cmd.Parameters.AddWithValue("@UserID", myuser.UserName)
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        
        Try
            con.Open()
            Dim sdr As SqlDataReader = cmd.ExecuteReader()
            While sdr.Read()

                UserIDLabel.Text = sdr("UserID").ToString()
                KioskFileLabel.Text = sdr("kFile").ToString()

                Const server As String = "http://192.168.3.50:81/_0/"
                Dim address As String = server + sdr("kFile")
                Dim file As New StreamReader(address, System.Text.Encoding.UTF8)
                
                FTPFileLabel.Text = file.ReadToEnd()
                
                file.Close()
                
            End While

        Finally

            con.Close()
            con.Dispose()
            
        End Try
    End Sub

解决方案

0) FTP or HTTP than?
1) StreamReader does not use any protocol handler. You can't use it to read a resource on an URL/URI.
2) Use WebRequest[^] class instead. It is quite sophisticated class, you can use it for both ftp and http requests.
+1) You still download. There is no actual difference in terms of http or ftp traffic if you save the retrieved content to a file, or not.


From code

Const server As String = "http://192.168.3.50:81/_0/"


I changed the code to

Dim request As WebRequest = WebRequest.Create("http://server/" + sdr("kFile"))
               Dim response As WebResponse = request.GetResponse()

               Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)

               Dim dataStream As Stream = response.GetResponseStream()

               Dim reader As New StreamReader(dataStream)

               Dim responseFromServer As String = reader.ReadToEnd()

               Console.WriteLine(responseFromServer)



               FTPFileLabel.Text = responseFromServer

               reader.Close()
               response.Close()
           End While

       Finally

           con.Close()
           con.Dispose()


       End Try
   End Sub



and thanks to Zoltán Zörgő for the help.


这篇关于如何访问FTP(http://)服务器上的文件并使用其中的值(无下载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆