简体   繁体   English

从可变网址c#下载文件

[英]Download a file from a Variable URL c#

We have been requested to go and Download an order file from our customers site via a url. 我们被要求去通过URL从我们的客户站点下载订单文件。 I want to do something like this. 我想做这样的事情。

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource, fileName);        
}

But the URL will be variable as we have to specify the Date and Time within the URL we post. 但是该URL是可变的,因为我们必须在发布的URL中指定日期和时间。

And the File we download will be variable also. 而且我们下载的文件也将是可变的。

As I'm new to C# I would like some advise as to how to achieve this? 由于我是C#的新手,所以我想就如何实现这一点提供一些建议?

Thanks In Advance 提前致谢

It depends on how the URLs will be generated. 这取决于如何生成URL。 Do they follow a pattern? 他们遵循模式吗? Do you know them in advance? 你事先知道吗?

private void GetVariableFile(string remoteUri, string filename) {
string myStringWebResource = null;

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient()) {
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource, fileName);
    //Do stuffs        
    }
}

You might wanna pass the Uri & the file obtained to a method which will handle the download and the elaboration, or in case you need to return the result, change the return type so that it can return the info you need. 您可能希望将Uri和获得的文件传递给将处理下载和详细说明的方法,或者如果您需要返回结果,请更改返回类型,以便它可以返回您所需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM