I want to download a file from a given URL, and I thought the below code will help, but not sure how it will work or download the file:
var httpClient = new HttpClient();
var httpRequest = new HttpRequestMessage();
httpRequest.Method = HttpMethod.Get;
httpRequest.Content = new StringContent(string.Empty);
httpRequest.RequestUri = new Uri("https://samplesite.com/attachments=1234");
httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
httpRequest.Headers.TryAddWithoutValidation("Authorization", String.Format("Bearer {0}",""));
var httpResponse = await httpClient.SendAsync(httpRequest);
httpResponse.EnsureSuccessStatusCode();
var mediafile = await httpResponse.Content.ReadAsStringAsync();
However, mediafile
is returned with an HTML code (not sure why so) and I've no clue, how an image will be downloaded and saved? On the other hand following cURL command does exactly what is needed.
curl 'https://samplesite.com/attachments=1234' -H 'Authorization: Bearer ACCESS_TOKEN' > media_file.jpg
The above cURL command simply downloads the file to media_file.jpg
Any assistance?