Post request to download file of a webserver from a WPF App (C#)

In the following code, I can send a POST request for the webserver and get a response:

private static readonly HttpClient client = new HttpClient();

public async static Task<int> User(string email, string password)
{
    email = email.ToLower();
    string theEmail = Cryptor.Encrypt(email);
    string thePass = Cryptor.Encrypt(password);
    try
    {
        var values = new Dictionary<string, string>
        {
            { "email", theEmail },
            { "password", thePass }
        };

        var content = new FormUrlEncodedContent(values);

        var response = await client.PostAsync("https://url...", content);

        var responseString = await response.Content.ReadAsStringAsync();

        Globals.USER = JsonConvert.DeserializeObject<UserObject>(responseString);
        return 1;
    }
    catch (Exception)
    {
        return 3;
    }
}

Is there a way to get a file sending a POST request and then save this file in a specific folder in the user's computer?

(What would be the PHP code to return the file after receiving the user's credentials and how would I get this file in the C# code?)

For example:

<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){
    $email = $_POST['email'];
    $password = $_POST['password'];
    // Validate user
    //  .
    //  .
    //  .
    // Until here it's ok

    // Now what would be the code to return the file?
    // For example, a file from the following path: "user-folder/docs/image.png"

}else{
    echo 'error';
    die;
}

And in the WPF App, in C# usually I would read the response like this:

var response = await client.PostAsync("https://url...", content);
var responseString = await response.Content.ReadAsStringAsync();

But how would it be to get the file back?


Sending files is generally done by transferring the content as binary data. If you don't explicitly send text data, using HttpClient.ReadAsString is useless. Either read the response content as byte array or as stream.

Sending the file using the readfile() function:

$file = 'user-folder/docs/image.png';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' .filesize($file));
readfile($file);
exit;

There are alternatives e.g., using cURL etc.

To request and save the file on your C# client you can handle the response content directly as Stream or byte array:

var response = await httpClient.PostAsync("https://url...", content);
var destinationFilePath = "image.png";
await using var destinationStream = File.Create(destinationFilePath);

// Handle the response directly as Stream
await using Stream sourceStream = await response.Content.ReadAsStreamAsync();
await sourceStream.CopyToAsync(destinationStream);

// Alternatively, create the Stream manually
// or write the byte array directly to the file
byte[] sourceData = await response.Content.ReadAsByteArrayAsync();
await destinationStream.WriteAsync(sourceData);