public UploadString ( System address, string data ) : string | ||
address | System | |
data | string | |
return | string |
using System.Net; using System.Text.Json; WebClient client = new WebClient(); client.Headers[HttpRequestHeader.ContentType] = "application/json"; var data = new { name = "John", age = 25 }; string json = JsonSerializer.Serialize(data); string response = client.UploadString("http://example.com/api/upload", json); Console.WriteLine(response);
using System.Net; WebClient client = new WebClient(); byte[] fileData = File.ReadAllBytes("C:/example.txt"); string response = client.UploadString("http://example.com/api/upload", Convert.ToBase64String(fileData)); Console.WriteLine(response);In this example, we read the contents of a file into a byte array and convert it to a Base64-encoded string using the Convert.ToBase64String method. We then upload the string using the WebClient.UploadString method. The use of the WebClient class indicates that the code is running on .NET Framework. The WebClient class is located in the System.Net namespace which is part of the core .NET Framework library.
public UploadString ( System address, string data ) : string | ||
address | System | |
data | string | |
return | string |