从URL下载文件,需要参数和身份验证 [英] Download file from URL with parameters and authentication required
问题描述
很抱歉打扰您,但我尝试了一个多星期,却没有找到实现此目标的简单有效的方法,因此,我在这里寻求您的帮助.我的工作中有一个经常执行的任务,请按照以下步骤操作:
Sorry for disturbing you, but I tried during more than a week and did no find a simple and efficient way to reach this goal, so I'm here to ask your help. I have a recurrent task in my job that follow this steps:
- 访问我公司的网站;
- 登录;
- 进行搜索;
- 从搜索结果页面下载KMZ文件.
我每周都要这样做,每次需要下载100多个文件,
I do it every week and need to download more then 100 files per time, do you know?
我有一个列出所有需要的结果的列表,因此我在c#中创建了一个应用程序来自动执行此过程,但是下载文件时,它不是正确的类型(KMZ),其内容是登录页面源代码.发生这种情况是因为我没有正确的文件名,它是由URL中的某些参数加载的,例如
I have a list with all result I need, so I created an application in c# to automate this process, but when the file is downloaded it is not the correct type (KMZ) and its content is the login page source code. It happens because I don't have the correct file name, it is loaded by some parameters in URL like https://mycompanywebsite.org/files/fileViewServlet?Parameter1=abx&Parameter2=xyz&Parameter3=123
那是我所做的.
//Download the file for the returned list
foreach (DataGridViewRow row in dataGridView1.Rows)
{
using (WebClient client = new WebClient())
{
//client.Credentials = new NetworkCredential("username", "password");
String credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("myusernae" + ":" + "mypassword"));
client.Headers[HttpRequestHeader.Authorization] = $"Basic {credentials}";
try
{
//Download the KMZ
client.Proxy = null;
client.QueryString.Add("parameter1", "value");
client.QueryString.Add("parameter2", "value");
client.QueryString.Add("parameter3", "value");
outputFileName = "File_Name_" + row.Cells["FieldTitle"].Value.ToString() + ".kmz";
client.DownloadFile("https://mycompanywebsite.org/files/fileViewServlet?", strTargetFolder + nomeArquivoEstaca);
}
catch (WebException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
如果您有任何样本可以帮助我实现这一目标,我将不胜感激.
If you have any sample to help me reach this goal, I'll really appreciate it.
非常感谢.
推荐答案
没有人可以实现这个目标吗?这里的主要问题是我无法访问URL上的文件名.如我所见,当调用具有该参数的URL时,该文件是从服务器操作中下载的.我可以使用某些函数来查找文件名并将其用于在C#Windows或Web应用程序中下载吗?
Nobody has a way that I can reach this goal? The major problem here is the fact that I have no access to the file name on the URL. As I can see, the file is downloaded from a server action when the URL with that parameters is called. Is some function that I can use to find the name of the file and use it do download it in c# windows or web application?
这篇关于从URL下载文件,需要参数和身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!