Below is a sample C# code for integrating with the FraudGuard.io API using HttpClient and associated explanation.

This C# code performs the following steps:

  1. Sets the FraudGuard.io API credentials (username and password).
  2. Defines the IP address to query.
  3. Constructs the API endpoint URL.
  4. Creates an instance of HttpClient.
  5. Sets the basic authentication header using Base64 encoding.
  6. Sends a GET request to the API endpoint.
  7. Checks if the request was successful.
  8. Reads and prints the response content if the request was successful.
  9. Prints an error message if the request was unsuccessful.
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace FraudGuardAPIClient
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // FraudGuard.io API credentials
            string username = "username";
            string password = "password";

            // IP address to query
            string ipAddress = "8.8.8.8";

            // API endpoint
            string apiUrl = $"https://api.fraudguard.io/ip/{ipAddress}";

            // Create HttpClient
            using (HttpClient client = new HttpClient())
            {
                // Set basic authentication header
                string authString = $"{username}:{password}";
                string base64AuthString = Convert.ToBase64String(Encoding.ASCII.GetBytes(authString));
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", base64AuthString);

                // Send GET request
                HttpResponseMessage response = await client.GetAsync(apiUrl);

                // Check if request was successful
                if (response.IsSuccessStatusCode)
                {
                    // Read response content
                    string responseBody = await response.Content.ReadAsStringAsync();

                    // Print response
                    Console.WriteLine(responseBody);
                }
                else
                {
                    Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
                }
            }
        }
    }
}

Make sure to replace ‘username’ and ‘password’ with your actual API credentials.