protected async void Button1_Click(object sender, EventArgs e)
{
using (var httpClient = new HttpClient())
{
var uri = new Uri(baseUrl + "/api/xmlsaa");
//var XML = "<XMLComputerInfo><Processor>AMD</Processor><HardDrive>Toshiba</HardDrive></XMLComputerInfo>";
var XML = TextBox1.Text;
var httpContent = new StringContent(XML, Encoding.UTF8, "text/xml");
var response = await httpClient.PostAsync(uri, httpContent);
}
}
API Server:
[HttpPost]
public HttpResponseMessage ReceiveXML([FromBody] XMLComputerInfo result)
{
if (result != null)
{
HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
response.Content = new ObjectContent<XMLComputerInfo>(result, new XmlMediaTypeFormatter());
return response;
}
else
{
HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
response.Content = new ObjectContent<string>("null object", new XmlMediaTypeFormatter());
return response;
}
}
WebApiConfig:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
GlobalConfiguration.Configuration.Formatters.Clear();
//GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.XmlMediaTypeFormatter());
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
}
public class XMLComputerInfo
{
[XmlElement("Processor")]
public string Processor { get; set; }
[XmlElement("HardDrive")]
public string HardDrive { get; set; }
}
Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from
scratch along with a detailed description about the results including any exception messages.
Best Regards,
Yong Lu
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Will this work for any WebApi or Rest for xml as input?
protected async void Button1_Click(object sender, EventArgs e)
{
using (var httpClient = new HttpClient())
{
var uri = new Uri(baseUrl + "/api/xmlsaa");
//var XML = "<XMLComputerInfo><Processor>AMD</Processor><HardDrive>Toshiba</HardDrive></XMLComputerInfo>";
var XML = TextBox1.Text;
var httpContent = new StringContent(XML, Encoding.UTF8, "text/xml");
var response = await httpClient.PostAsync(uri, httpContent);
}
}
Will this work for any WebApi or Rest for xml as input?
protected async void Button1_Click(object sender, EventArgs e)
{
using (var httpClient = new HttpClient())
{
var uri = new Uri(baseUrl + "/api/xmlsaa");
//var XML = "<XMLComputerInfo><Processor>AMD</Processor><HardDrive>Toshiba</HardDrive></XMLComputerInfo>";
var XML = TextBox1.Text;
var httpContent = new StringContent(XML, Encoding.UTF8, "text/xml");
var response = await httpClient.PostAsync(uri, httpContent);
}
}
Hi Guhananth,
There is no omnipotent code here, you need to change the code appropriately according to your own situation.
Why don't you try it on your side? You need to try to write your own code based on Community members' advice.
Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch
along with a detailed description about the results including any exception messages.
Best Regards,
Yong Lu
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
6 Points
246 Posts
Consume Web api for Get,post,delete,head,option,merge using xml as input
Feb 12, 2019 06:16 PM|Guhananth|LINK
Hi,
I have the following:
Anand
Star
11464 Points
2439 Posts
Re: Consume Web api for Get,post,delete,head,option,merge using xml as input
Feb 13, 2019 02:53 AM|Yohann Lu|LINK
I have made a sample on my side. You can refer it.
Client:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendXMLstring.aspx.cs" Inherits="API2_Temple.SendXMLstring" validateRequest="false" Async="true" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" Width="654px"><XMLComputerInfo><Processor>AMD</Processor><HardDrive>Toshiba</HardDrive></XMLComputerInfo></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send XML" /> </div> </form> </body> </html>
Client.cs:
API Server:
WebApiConfig:
Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.
Best Regards,
Yong Lu
Member
6 Points
246 Posts
Re: Consume Web api for Get,post,delete,head,option,merge using xml as input
Feb 13, 2019 04:44 PM|Guhananth|LINK
Hi,
Will this work for any WebApi or Rest for xml as input?
Star
11464 Points
2439 Posts
Re: Consume Web api for Get,post,delete,head,option,merge using xml as input
Feb 14, 2019 02:43 AM|Yohann Lu|LINK
Hi Guhananth,
There is no omnipotent code here, you need to change the code appropriately according to your own situation.
Why don't you try it on your side? You need to try to write your own code based on Community members' advice.
Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.
Best Regards,
Yong Lu