在C#Windows App中通过http下载文件 [英] Download file through http in C# Windows App

查看:111
本文介绍了在C#Windows App中通过http下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#Visual Studio 2010 .Net 4开发一个Windows应用程序。我的任务之一是在提交URL时将文件下载到我的本地文件夹。问题是给定的HTTP URL中没有文件名。该文件是动态生成的,它显示另存为对话框,在那里我可以看到文件名。



这是系统托盘应用程序,可在自动驾驶的基础上运行,无任何用户干扰。因此,我需要完成下载文件的任务不知道它的名字:(



希望我的问题现在清楚了!!!

I am developing an windows application using C# Visual Studio 2010 .Net 4. In which one of my task is to download a file to my local folder on submitting an URL. The problem is the given HTTP URL doesn't have a file name in it. The file is generated dynamically and it shows "Save As" dialog box, where I could see the file name.

This is system tray application and runs on auto pilot basis without any user interference. Hence, I need to achieve the task of downloading the file "without knowing its name" :(

Hope my question is clear now !!!

推荐答案

如果您请求了该文件(我假设通过 HttpWebRequest WebClient 对象),你应该知道文件的名称。在任何情况下,只需用临时名称保存文件,提示用户输入他们想要使用的名称,并重命名临时文件。
If you requested the file (I assume via the HttpWebRequest or WebClient objects), you should know the name of the file. In any case, just save the file with a temporary name, prompt the user for the name they want to use, and rename the temporary file.


检查一下,它应该让你开始使用一个解决方案:



Check this out, it should get you started with a solution:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dropshipdirect.com/warehouse/vendor_export_speedup.php?use_link=on&ve_sid=fal9vgtv5hg3yo4ofhiqhiuogmy3dbji");
WebResponse response = request.GetResponse();
string containsfilename = response.Headers[1];


显而易见的解决方案是建立一个SaveFileDialog。如果你不想与用户互动,你将必须组成一个文件名,例如通过将UID附加到URL的路径部分,或者使用查询参数执行某些操作。



阅读评论,您似乎还有另一个问题,即您实际查看的页面不是您要下载的文件。您可能必须抓取该页面,查看它的真实链接,并发出另一个HTTP请求来获取该文件(并且不要忘记该文件的Referer标题)。
The obvious solution is to put up a SaveFileDialog. If you want no user interaction, you will have to make up a file name, for example by appending a UID to the path part of the URL, or doing something with the query parameters.

Reading the comments, it seems that you have another issue, which is that the page you're actually looking at is not the file you want to download. You may have to grab that page, look through it for the 'real' link, and make another HTTP request to get the file (and don't forget the Referer header on that one).


这篇关于在C#Windows App中通过http下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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