我的工作中有一项经常性任务,请按照以下步骤操作:
我每周都会这样做,每次需要下载超过100个文件,你知道吗?
我有一个包含我需要的所有结果的列表,因此我用 C# 创建了一个应用程序来自动执行此过程,但是当下载文件时,它的类型不正确 (KMZ),其内容是登录页面源代码。发生这种情况是因为我没有正确的文件名,它是由 URL 中的一些参数加载的,例如 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());
}
}
}
没有人有办法让我达到这个目标吗?这里的主要问题是我无法访问 URL 上的文件名。正如我所看到的,当调用带有该参数的 URL 时,会从服务器操作下载该文件。我可以使用某些函数来查找文件名并使用它在 C# Windows 或 Web 应用程序中下载它吗?