0

I have the need to convert this PS cmdlet to C#

invoke-webrequest -uri [uri] -method GET -headers [myHeader] -outfile  [myFile]

where [uri] is the download link, [myHeader] contains my apikey and my outfile is the name of the destination file.

The invoke-webrequest in PS works but my project requires C# code. I can use the following code for the normal get or post action if I were dealing with the standard json:

        var msg = new HttpRequestMessage(HttpMethod.Get, [uri]);
        msg.Headers.Add(_apiKeyTag, _myKey);
        var resp = await _httpClient.SendAsync(msg);

Assuming _httpClient is created by new HttpClient and assuming the download link [uri] exists. The file to be downloaded is either a pdf, jpg, img or csv file. I am not sure how to convert the above comdlet in PS to C#.

How do I specify my destination file? (I am referring to the option -outfile in PS)

user1205746
  • 3,110
  • 11
  • 44
  • 73
  • `WebClient` is the old implementation - it uses `HttpClient` under the covers. – Daniel A. White Aug 21 '20 at 03:54
  • Does this answer your question? [Download file with WebClient or HttpClient?](https://stackoverflow.com/questions/45711428/download-file-with-webclient-or-httpclient) – devNull Aug 21 '20 at 03:57
  • @devNull: No, not really. The site I would like to download the file from requires an apikey to download, how do I put the apikey ? Basically, I would like to see if there is a way to do what the ps cmdlet is doing. – user1205746 Aug 21 '20 at 04:01
  • @user1205746 By adding it to the `Headers` on the `HttpClient`, as you're already doing in your example – devNull Aug 21 '20 at 04:05
  • @devNull: then how do I specify the destination file if I use httpclient? This is my main road block .. the equivalent to -outfile in powershell – user1205746 Aug 21 '20 at 04:06
  • @user1205746 By copying the content to a file stream, as showed in the answer to the question I linked above – devNull Aug 21 '20 at 04:07
  • https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts – mjwills Aug 21 '20 at 04:08

1 Answers1

3

Never use anything but HttpClient. If you catch yourself typing WebClient of anything other than HttpClient, kindly slap your hand away from the keyboard.

You want to download a file with HttpClient? Here is an example of how to do that:

private static readonly HttpClient _httpClient = new HttpClient();

private static async Task DoSomethingAsync()
{
    using (var msg = new HttpRequestMessage(HttpMethod.Get, new Uri("https://www.example.com")))
    {
        msg.Headers.Add("x-my-header", "the value");
        using (var req = await _httpClient.SendAsync(msg))
        {
            req.EnsureSuccessStatusCode();
            using (var s = await req.Content.ReadAsStreamAsync())
            using (var f = File.OpenWrite(@"c:\users\andy\desktop\out.txt"))
            {
                await s.CopyToAsync(f);
            }
        }
    }
}

You can do anything you want with HttpClient. No reason to use RestClient, WebClient, HttpWebRequest or any of those other "wannabe" Http client implementations.

Andy
  • 12,859
  • 5
  • 41
  • 56
  • 1
    Thanks again Andy. People like yourself who are willing to give precise instruction is the reason that makes this site great. Can not thank you enough! – user1205746 Aug 21 '20 at 04:21
  • @user1205746 -- you got it. There is a massive amount of things to learn about `HttpClient`. This is just the tip of the ol' ice burg. It may be frustrating at times, but there are plenty of resources to help you out. – Andy Aug 21 '20 at 04:22
  • 1
    exactly! I read the Microsoft documentation and one led to the other with no end, especially if you start at the wrong side of the documentation. Your instruction guides me to read the correct documentation and is a good stepping stone for me.. again, much appreciated! – user1205746 Aug 21 '20 at 04:26