create an api call to google in csharp

To create an API call to Google in C#, you can use the HttpClient class from the System.Net.Http namespace. Here's an example code snippet that calls the Google Maps Geocoding API to get the latitude and longitude of an address:

main.cs
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        using HttpClient client = new HttpClient();
        string address = "1600 Amphitheatre Parkway, Mountain View, CA";
        string apiKey = "YOUR_API_KEY"; // replace with your own Google API key
        string url = $"https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={apiKey}";

        HttpResponseMessage response = await client.GetAsync(url);
        if (response.IsSuccessStatusCode)
        {
            string responseBody = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseBody);
            // parse the JSON response to get the latitude and longitude
        }
        else
        {
            Console.WriteLine($"Error: {response.StatusCode}");
        }
    }
}
874 chars
27 lines

In this example, we're using the HttpClient.GetAsync method to send an HTTP GET request to the Google Maps Geocoding API. We're passing in the address we want to geocode and our API key as query parameters in the URL string.

Once we get the response back, we read the response body as a string and output it to the console. In a real application, you would parse the JSON response to extract the data you need, such as the latitude and longitude.

gistlibby LogSnag