0

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?

Aniruddha
  • 837
  • 3
  • 13
  • 37
  • this may help you: https://stackoverflow.com/a/71949994/1529246 – YK1 May 06 '23 at 05:40
  • Firstly, why would you think that reading the content as a string would help if you want to download an image file? I've never actually used a `HttpClient` but, after glancing at the documentation, which you should have read in detail, it seems to me that calling `GetByteArrayAsync` or `GetStreamAsync` would be the logical options. You could then call `File.WriteAllBytes` to save to a file or `Image.FromStream` to create an `Image` object. – jmcilhinney May 06 '23 at 06:09
  • @jmcilhinney I had tried `ReadAsStreamAsync()` to no success and the answer suggested by @YK1 also doesn't help. I get a corrupted output file. Still trying though. – Aniruddha May 06 '23 at 06:22
  • 1
    `httpRequest.Content.Headers.ContentType` setting this tells the server you are sending some JSON with your request, which you are not. This might result in a BadRequest or similar problem so I would remove that. Also, if you add `-v` to your curl command you can see what headers it is sending (and receiving) if you want to make sure that you replicate it properly. – Karl-Johan Sjögren May 06 '23 at 06:31
  • I was about to post an answer when the question was closed. I tested what I suggested in my previous comment and both options worked exactly as expected. You appear to be complicating things unnecessarily. Just create the `HttpClient` and call one of the methods I specified directly on it. Note that `ReadAsStreamAsync` is not one of those methods. I called `File.WriteAllBytesAsync` for the `byte` array and `File.Create` and `Stream.CopyToAsync` for the `Stream`. Both files were created correctly. – jmcilhinney May 06 '23 at 09:15
  • @jmcilhinney, agreed whatever you mentioned is correct and I saw various examples around it and they all seem to be same. Unfortunately, nothing works, a file is created but that's always corrupted or incomplete. Let me elaborate, I'm trying to do something as mentioned here in 'Download Media': https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media , if you refer to 'Download Media' section it says : `If you directly click on the URL you get from a /MEDIA_ID GET call, you get an access error.` Perhaps, is that reason why it doesn't work from C# code? – Aniruddha May 06 '23 at 14:31
  • @jmcilhinney And I figured out the solution, it works now. Extra header need to set in order to make it work `httpClient.DefaultRequestHeaders.Add("User-Agent", "C# App");` – Aniruddha May 06 '23 at 17:07

1 Answers1

0

I think you can use 'ReadAsStreamAsync' Method to get image by stream, then save the stream to local file

 var medisStream = await httpResponse.Content.ReadAsStreamAsync();

 using var fileStream = new FileStream("d://xxx.jpg", FileMode.Create);
 await medisStream.CopyToAsync(fileStream);
leon
  • 1