'How can I "click" a url programatically from code behind

I have a dynamic list of URL's, which I assemble in code behind in a .NET web app. Each URL simply downloads a file when clicked or run in the address bar of a browser. I need to automate this, in other words loop through the list, and for each url I need to programmatically "click" the url to initiate the download. Any help will be greatly appreciated.



Solution 1:[1]

You can call a url with HttpClient Class

this is my sample for call url and download file, hope this helps.

    public async Task RequestAsync(string url, string jsonContent, HttpMethod HttpMethod, Dictionary<string, string> headers = null, bool XRequestedWithHeader = false,
            string requestHeaderType = "application/json")
        {
            var client = new HttpClient();
            HttpResponseMessage response;
            //get url
            if (HttpMethod == HttpMethod.Get)
            {

                response = await client.GetAsync(url);
            }
            //post url
            else if (HttpMethod == HttpMethod.Post)
            {
                //convert json to bytes
                var buffer = System.Text.Encoding.UTF8.GetBytes(jsonContent);
                //buffer bytes
                var byteContent = new ByteArrayContent(buffer);
                //set header content
                byteContent.Headers.ContentType = new MediaTypeHeaderValue(requestHeaderType);
                if (headers != null)
                    foreach (var header in headers)
                    {
                        byteContent.Headers.Add(header.Key, header.Value);
                    }

                //headers
                if (XRequestedWithHeader)
                    byteContent.Headers.Add("X-Requested-With", "XMLHttpRequest");
                //post url and content
                response = await client.PostAsync(url, byteContent);
            }
            else
            {
                throw new Exception("Invalid Request Method");
            }

            if (response.IsSuccessStatusCode)
            {
                Response.AddHeader("Content-disposition", "attachment; filename=" & ShortFileName)
Response.ContentType = "application/vnd.ms-excel"
Response.WriteFile(FileName)
Response.End()
            }

        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 seyyed mohammad hosein kashfi