您好,最后我找到了一个POST API,它以XML格式发送和接收数据。
我的API有一个请求正文-
我感到困惑,因为我应该如何从代码中使用此主体来击中api-
我为此创建了一个代理类-
这是我为访问API而编写的代码-
我该如何进行?
我的API有一个请求正文-
C#:
<Person>
<Id>12345</Id>
<Customer>John Smith</Customer>
<Quantity>1</Quantity>
<Price>10.00</Price>
</Person>
我感到困惑,因为我应该如何从代码中使用此主体来击中api-
我为此创建了一个代理类-
C#:
[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;
}
}
}
这是我为访问API而编写的代码-
C#:
var request = (HttpWebRequest)WebRequest.Create("//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 not sure about this one.
我该如何进行?
Last edited: