How to send HTTP Post request in C# without JSON content

So I need to sent an HTTP Post request in C# (.NET) without JSON parameters because they are already in message.SentUrl (That’s just how my boss wants me to do it) and if the response is 200 return true, else return false. Is there any way I can do it without making my function async?

public static bool SendMessage(SpoolMessage message)    
{
    message.SentUrl = ReplaceUrlParameters(message);
 
    // Make post request here and return true if success else return false
 
}

If you need to send an HTTP POST request in C# without using async and return true for a 200 response code (or false otherwise), you can use synchronous HTTP calls with the HttpClient class or the older HttpWebRequest. Here’s how you can do it using HttpClient:

Synchronous HTTP Post Request with HttpClient

public static bool SendMessage(SpoolMessage message)
{
    message.SentUrl = ReplaceUrlParameters(message);

    try
    {
        using (var client = new HttpClient())
        {
            // Set up the HttpClient with the necessary headers
            var response = client.PostAsync(message.SentUrl, null).GetAwaiter().GetResult();

            // Check the status code and return the result
            return response.IsSuccessStatusCode; // True if status code is 2xx
        }
    }
    catch (Exception ex)
    {
        // Log the exception if necessary
        Console.WriteLine($"Error sending message: {ex.Message}");
        return false;
    }
}

Explanation:

  1. PostAsync: Normally, PostAsync is asynchronous, but .GetAwaiter().GetResult() makes it run synchronously. This avoids making your function async, as per your requirement.
  2. Response Check: response.IsSuccessStatusCode checks if the status code is in the 2xx range.
  3. Null Content: Since you’re not sending any JSON or additional parameters, the second argument to PostAsync is null.

Notes:

  • Thread Blocking: Using .GetAwaiter().GetResult() can block the calling thread, which might not be ideal in a highly concurrent or UI application.
  • Error Handling: Add appropriate error handling for HTTP errors (e.g., HttpRequestException) or connection issues.
  • Legacy Option: If your environment doesn’t support HttpClient, consider using the older HttpWebRequest, but HttpClient is preferred for most cases.

Alternative with HttpWebRequest (Older API):

public static bool SendMessage(SpoolMessage message)
{
    message.SentUrl = ReplaceUrlParameters(message);

    try
    {
        var request = (HttpWebRequest)WebRequest.Create(message.SentUrl);
        request.Method = "POST";

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            return response.StatusCode == HttpStatusCode.OK;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error sending message: {ex.Message}");
        return false;
    }
}

This approach is fully synchronous and does not require async or await. However, HttpWebRequest is considered outdated and should only be used if you have constraints that prevent using HttpClient.