ASP.NET Tutorial/Development/FtpWebRequest

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Using an FtpWebRequest to download a file from an FTP site (C#)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Uri uri = new Uri("ftp://ftp.microsoft.ru/ReadMe.txt ");
        if (uri.Scheme == Uri.UriSchemeFtp)
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();
            response.Close();
            this.Panel1.GroupingText = tmp;
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" 
            Height="355px" Width="480px" ScrollBars="Auto">
        </asp:Panel>
    
    </div>
    </form>
</body>
</html>


Using an FtpWebRequest to download a file from an FTP site (VB)

<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim uri As New Uri("ftp://ftp.microsoft.ru/ReadMe.txt")
        If (uri.Scheme = uri.UriSchemeFtp) Then
            Dim request As FtpWebRequest = FtpWebRequest.Create(uri)
            request.Method = WebRequestMethods.Ftp.DownloadFile
            Dim response As FtpWebResponse = request.GetResponse()
            Dim reader As New StreamReader(response.GetResponseStream())
            Dim tmp As String = reader.ReadToEnd()
            response.Close()
        
            Me.Panel1.GroupingText = tmp
        End If
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" 
            Height="355px" Width="480px" ScrollBars="Auto">
        </asp:Panel>
    
    </div>
    </form>
</body>
</html>