Overview
The Universal Windows Platform provides several APIs that simplify common networking tasks while remaining performant and secure. Choose the right tool based on your scenario:
- HttpClient – High‑level HTTP/HTTPS communication.
- StreamSocket – Low‑level TCP/UDP socket control.
- NetworkInformation – Query network adapters, connectivity, and cost.
HttpClient
Use Windows.Web.Http.HttpClient for REST services, file downloads, and any HTTP‑based communication. It supports async calls, automatic response buffering, and built‑in authentication.
using Windows.Web.Http;
public async Task<string> GetJsonAsync(string url)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
HttpResponseMessage response = await client.GetAsync(new Uri(url));
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
Sockets
When you need granular control over data streams or want to implement custom protocols, Windows.Networking.Sockets.StreamSocket is the choice.
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
public async Task SendMessageAsync(string host, string message)
{
var socket = new StreamSocket();
await socket.ConnectAsync(new HostName(host), "8080");
DataWriter writer = new DataWriter(socket.OutputStream);
writer.WriteString(message);
await writer.StoreAsync();
writer.DetachStream();
socket.Dispose();
}
NetworkInformation
Determine if the device is online, what network interfaces are available, and the cost of the connection.
using Windows.Networking.Connectivity;
public bool IsInternetAvailable()
{
var profile = NetworkInformation.GetInternetConnectionProfile();
return profile != null &&
profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
}
Sample: Download JSON and display in UI
This example combines HttpClient with JsonObject to fetch and show data.
using Windows.Web.Http;
using Windows.Data.Json;
public async Task LoadWeatherAsync()
{
string endpoint = "https://api.openweathermap.org/data/2.5/weather?q=Seattle&appid=YOUR_KEY";
string json = await GetJsonAsync(endpoint);
JsonObject obj = JsonObject.Parse(json);
string description = obj.GetObject("weather")
.GetArray("0")
.GetObject()
.GetNamedString("description");
WeatherTextBlock.Text = $"Current: {description}";
}
Remember to add the Internet (Client) capability in Package.appxmanifest.