C# Code Example
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:
- Sets the FraudGuard.io API credentials (username and password).
- Defines the IP address to query.
- Constructs the API endpoint URL.
- Creates an instance of HttpClient.
- Sets the basic authentication header using Base64 encoding.
- Sends a GET request to the API endpoint.
- Checks if the request was successful.
- Reads and prints the response content if the request was successful.
- 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.