问题
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:
- Access my company website;
- Sign-in;
- Make a search;
- Download a KMZ file from the search result page.
I do it every week and need to download more then 100 files per time, do you know?
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
That's what I did.
//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.
Thanks very much.
回答1:
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?
来源:https://stackoverflow.com/questions/52764470/download-file-from-url-with-parameters-and-authentication-required