【发布时间】:2020-10-25 23:22:12
【问题描述】:
我是 API 新手。我有一个休息 API,它有一个 XML 格式的请求正文和响应正文。 我想点击 API,但我不知道如何从代码中发送请求正文。 我的 API 的请求正文是 -
<Person>
<Id>12345</Id>
<Customer>John Smith</Customer>
<Quantity>1</Quantity>
<Price>10.00</Price>
</Person>
我的努力:
到目前为止,我知道要处理 API,您必须创建一个代理类。所以我的代理类是-
[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;
}
}
}
从这个答案
How can I Post data using HttpWebRequest?
我正在做以下事情-
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);
var data = Encoding.ASCII.GetBytes(person) 出现错误
上面写着cannot convert form Person to Char[]
我现在不确定如何继续。
【问题讨论】: