编辑: 显然我有什么工作。我一直在使用从序列化对象生成的xml进行测试,该序列化对象不起作用。我正在更多地讨论它,并决定尝试使用我创建的动态XML(与此处发布的相同),并且它工作正常。我认为也许是因为当我在使用相同的命名空间之前以这种方式尝试它时... 但是,如何使用序列化对象中的xml不起作用?我注意到当它由对象创建时,它在xml中使用不同的名称空间。这是为什么?REST API服务中的对象作为参数问题
原文章: 我有一个将自定义对象传递给我创建的REST API的问题。现在我只是试图通过发布Lead类型的对象到WebInvoke方法来测试它。 我的服务是用WCF编写的。 Lead对象位于一个单独的类库项目中,将由我的业务逻辑层完成。
我使用常规的ASP .net页面发布。您将在下面的代码段中看到,我尝试了以两种方式手动构建xml,并且还将该对象序列化为xml并将其用作POST数据。都不工作,我得到这个错误: 远程服务器返回一个错误:(400)错误的请求。
我知道服务配置正确,因为我可以调用并从我写的其他WebInvoke方法获得响应,这是一种GET方法。
这里是卫浴套间代码: 服务接口代码:
[ServiceContract(Namespace = "http://www.mytestnamespace.com/test")]
public interface IRESTService
{
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "leads",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
string AddLead(Lead lead);
[OperationContract]
[WebInvoke(Method = "GET",
UriTemplate = "lead/{id}",
ResponseFormat = WebMessageFormat.Xml)]
string LeadData(string id);
}
服务代码:
public class RESTService : IRESTService
{
public string AddLead(Lead lead)
{
return "AddLead Hit";
}
public string LeadData(string id)
{
return "LeadData was hit, id=" + id;
}
}
铅对象代码:
[DataContract(Name="Lead",
Namespace = "http://www.mytestnamespace.com/test")]
public class Lead
{
[DataMember(Name="FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
[DataMember(Name = "Email")]
public string Email { get; set; }
[DataMember(Name = "Phone")]
public string Phone { get; set; }
}
最后我是代码在aspx页面上使用以发布数据:
try
{
Lead l = new Lead();
l.FirstName = "John";
l.LastName = "Doe";
l.Email = "[email protected]";
l.Phone = "5555551234";
XmlSerializer ser = new XmlSerializer(typeof(Lead));
StringWriter sw = new StringWriter();
ser.Serialize(sw, l);
string s = sw.ToString();
/*XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement root = doc.CreateElement("Lead");
root.SetAttribute("xmlns", "http://www.mytestnamespace.com/test");
//root.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
doc.InsertBefore(dec, doc.DocumentElement);
doc.AppendChild(root);
XmlElement firstName = doc.CreateElement("FirstName");
XmlElement lastName = doc.CreateElement("LastName");
XmlElement email = doc.CreateElement("Email");
XmlElement phone = doc.CreateElement("Phone");
firstName.AppendChild(doc.CreateTextNode("John"));
lastName.AppendChild(doc.CreateTextNode("Doe"));
email.AppendChild(doc.CreateTextNode("[email protected]"));
phone.AppendChild(doc.CreateTextNode("8885551234"));
root.AppendChild(firstName);
root.AppendChild(lastName);
root.AppendChild(email);
root.AppendChild(phone);*/
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:54966/RESTService.svc/leads"));
req.Method = "POST";
req.ContentType = "application/xml";
//byte[] formData = UTF8Encoding.UTF8.GetBytes(doc.InnerXml);
byte[] formData = UTF8Encoding.UTF8.GetBytes(s);
req.ContentLength = formData.Length;
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
string result = null;
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
}
catch (Exception exc)
{
throw exc;
}
您正在使用XmlSerializer来序列化您的Lead对象,并猜测您的服务使用DataContractSerializer对其进行反序列化,因此您会收到400错误的请求错误。尝试在对象上使用DataContractSerializer。 – Rajesh 2012-01-18 10:15:16