Download file from URL with parameters and authentication required

主宰稳场 提交于 2019-12-24 22:43:02

问题


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:

  1. Access my company website;
  2. Sign-in;
  3. Make a search;
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!