[英]How to send a request body in XML using HttpWebRequest
I am new to APIs.我是 API 的新手。 I have a rest API which have a request body and response body in XML format.我有一个 rest API,它有一个 XML 格式的请求正文和响应正文。 I want to hit the API but I have no clue how to send the request body from the code.我想打 API 但我不知道如何从代码发送请求正文。 The request body of my API is -我的API的request body是——
<Person>
<Id>12345</Id>
<Customer>John Smith</Customer>
<Quantity>1</Quantity>
<Price>10.00</Price>
</Person>
My Effort:我的努力:
I know so far that to deal with APIs you have to create a proxy class. So my proxy class is -到目前为止我知道要处理 API,你必须创建一个代理 class。所以我的代理 class 是 -
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Person
{
private ushort idField;
private string customerField;
private byte quantityField;
private decimal priceField;
/// <remarks/>
public ushort Id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
/// <remarks/>
public string Customer
{
get
{
return this.customerField;
}
set
{
this.customerField = value;
}
}
/// <remarks/>
public byte Quantity
{
get
{
return this.quantityField;
}
set
{
this.quantityField = value;
}
}
/// <remarks/>
public decimal Price
{
get
{
return this.priceField;
}
set
{
this.priceField = value;
}
}
}
and from this answer从这个答案
How can I Post data using HttpWebRequest? 如何使用 HttpWebRequest 发布数据?
I am doing the following -我正在做以下 -
var request = (HttpWebRequest)WebRequest.Create("https://reqbin.com/sample/post/xml");
Person person = new Person();
Console.WriteLine("Enter ID");
person.Id = Convert.ToUInt16(Console.ReadLine());
Console.WriteLine("Enter Name");
person.Customer = Console.ReadLine();
Console.WriteLine("Enter Quantity");
person.Quantity = Convert.ToByte(Console.ReadLine());
Console.WriteLine("Enter Price");
person.Price = Convert.ToDecimal(Console.ReadLine());
var data = Encoding.ASCII.GetBytes(person);
I am getting error in var data = Encoding.ASCII.GetBytes(person)
我在var data = Encoding.ASCII.GetBytes(person)
中收到错误
It says cannot convert form Person to Char[]
它说cannot convert form Person to Char[]
I am not sure now how to proceed.我现在不确定如何进行。
GetBytes
expects string-like input to turn it into a byte array. GetBytes
期望类似字符串的输入将其转换为字节数组。 So you'll have to turn peron
into a string/char-array first.所以你必须peron
变成一个字符串/字符数组。 Since you want to work with XML, you should use an XML serializer.由于您想使用 XML,因此您应该使用 XML 序列化程序。 Eg using the serializer built into .NET:例如,使用 .NET 内置的序列化程序:
// Setup for the person above
// Serialize the person into an XML string
var serializer = new XmlSerializer(typeof(Person));
var sb = new StringBuilder();
using (XmlWriter xmlWriter = XmlWriter.Create(sb))
{
serializer.Serialize(xmlWriter, person);
}
// Byte array data to send
var data = Encoding.ASCII.GetBytes(sb.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.