public UploadValuesAsync ( System address, System data ) : void | ||
address | System | |
data | System | |
return | void |
using System; using System.Collections.Specialized; using System.Net; using System.Threading.Tasks; public static async Task UploadFormData() { NameValueCollection data = new NameValueCollection(); data["name"] = "John Doe"; data["email"] = "[email protected]"; data["message"] = "Hello, world!"; using (WebClient client = new WebClient()) { byte[] response = await client.UploadValuesAsync("https://example.com/contact", data); Console.WriteLine(Encoding.UTF8.GetString(response)); } }
using System; using System.Net; using System.Text; using System.Threading.Tasks; public static async Task SubmitJsonData() { string json = "{\"name\":\"John Doe\",\"email\":\"[email protected]\",\"message\":\"Hello, world!\"}"; using (WebClient client = new WebClient()) { client.Headers[HttpRequestHeader.ContentType] = "application/json"; byte[] response = await client.UploadDataTaskAsync("https://example.com/api/messages", Encoding.UTF8.GetBytes(json)); Console.WriteLine(Encoding.UTF8.GetString(response)); } }In this example, a JSON object is created as a string, and the UploadDataTaskAsync method is used to upload the data to the https://example.com/api/messages endpoint. The content type header is set to "application/json" using the WebClient's Headers property. The server's response is then printed to the console. Package library: System.Net.WebClient is part of the .NET Framework Class Library, which is included in the .NET runtime and does not require any external package dependencies.
public UploadValuesAsync ( System address, System data ) : void | ||
address | System | |
data | System | |
return | void |