3

有一个数据文件和一些图像文件,我必须每晚使用 asp.net 下载到我们的本地服务器。做这个的最好方式是什么?

更新

好的,在查看回复后,我发现我最初使用 asp.net 的帖子是一个糟糕的选择。您将如何为 C# 中的控制台应用程序编写它。我不确定我使用哪些类来连接远程服务器并从远程服务器下载文件。

谢谢

4

6 回答 6

9

“你将如何用 C# 为控制台应用程序编写它。”

创建 C# 控制台应用程序。添加对 System.Net 的引用。


using System;
using System.Net;

namespace Downloader
{
    class Program
    {
        public static void Main(string[] args)
        {
            using (WebClient wc = new WebClient())
            {
                wc.DownloadFile("http://www.mydomain.com/resource.img", "c:\\savedImage.img");
            }
        }
    }
}
于 2009-01-19T15:45:42.347 回答
1

要从远程 URL 下载任何文件,您可以在 System.Net 命名空间内的 C# 中使用 WebClient。

公共文件结果歌曲(字符串歌曲){

        WebClient client = new WebClient();
        var stream = client.OpenRead("http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3");

        return File(stream, "audio/mpeg");

    }

对于每晚下载数据,您可以使用任务调度程序。 http://www.quartz-scheduler.net/可以帮你安排任务

于 2016-06-20T17:28:57.143 回答
0

要每天晚上执行此操作,您可以将其设置为服务器上的计划任务,以访问特定的 asp.net 网页以执行您想要的代码。

您的 ASP.NET 页面将包含用于下载文件和执行所需处理的代码。

于 2009-01-19T15:02:55.693 回答
0

您通常不会通过 ASP.NET 执行此操作。网页代码仅在发出 HTTP/HTTPS 请求时执行,并且通常由最终用户使用 Web 浏览器或 Web 爬虫触发。

您想使用 FTP 之类的东西来下载文件,并使用 Windows 服务来自动启动下载。


更新以响应问题更新:

您可以通过简单的google 搜索找到大量有关通过 .NET 中的 FTP 下载的信息。

查看这个C# FTP 客户端库

这里有一些关于在 .NET 中创建 Windows 服务的不错的链接:

http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

http://www.developer.com/net/net/article.php/2173801

于 2009-01-19T15:05:56.250 回答
0

System.Net.WebClient 类上的 DownloadFile 方法可能是一个不错的起点。你给它一个 URL 字符串和一个文件名,剩下的就交给它了。 MSDN 上的 System.NET.WebClient

您甚至可以使用此类设置自定义用户代理字符串并上传文件。

于 2009-01-19T15:27:09.980 回答
0

如果你不能用 FTP 来做,你想使用 HttpWebRequest 和 HttpWebResponse 来完成这项工作。来自 MSDN

using System;
using System.Net;
using System.Text;
using System.IO;


    public class Test
    {
        // Specify the URL to receive the request.
        public static void Main (string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            Console.WriteLine (readStream.ReadToEnd ());
            response.Close ();
            readStream.Close ();
        }
    }

/*
The output from this example will vary depending on the value passed into Main 
but will be similar to the following:

Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>

*/
于 2009-01-19T15:32:40.080 回答