Hi @Noah Aas , Welcome to Microsoft Q&A,
You can use the HttpClient
class from the System.Net.Http
namespace. Here's a step-by-step guide to achieve this:
Create the XML content: You'll need to create an XML string that you want to send in the POST request.
Set up the HttpClient: Initialize an instance of the HttpClient
.
Create the HttpContent: Wrap the XML string in a StringContent
object and set the appropriate media type.
Send the POST request: Use the PostAsync
method of HttpClient
to send the request.
Handle the response: Process the response from the server.
Here's a complete example:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Your XML string
string xmlData = @"<Request><Data>Hello, World!</Data></Request>";
// The URL to send the POST request to
string url = "https://example.com/api/endpoint";
// Create a new HttpClient instance
using (HttpClient client = new HttpClient())
{
// Set the content type to XML
HttpContent content = new StringContent(xmlData, Encoding.UTF8, "application/xml");
// Send the POST request
HttpResponseMessage response = await client.PostAsync(url, content);
// Ensure the response was successful, or throw an exception
response.EnsureSuccessStatusCode();
// Read the response content
string responseContent = await response.Content.ReadAsStringAsync();
// Print the response
Console.WriteLine(responseContent);
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.