我在API上运行了一些本地测试,由于某种原因,数据XML数据没有进入API。 rData.labelDetail作为null提交给API。
看起来好像我使用了错误的ContentType,但我已经尝试过application / xml和text / xml但没有成功。
请求:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
public partial class TestAPI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebRequest request = WebRequest.Create("http://localhost:56146/Packages.svc/auth");
request.Method = "POST";
string postData = "<?xml version='1.0' encoding='UTF-8'?><RequestData xmlns='http://www.labels.com/label'><details>Test|Second Part</details></RequestData>";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/xml";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
}
服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace XYZService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IPackages" in both code and config file together.
[ServiceContract]
public interface IPackages
{
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "auth")]
ResponseData Auth(RequestData rData);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace XYZService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Packages" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Packages.svc or Packages.svc.cs at the Solution Explorer and start debugging.
public class Packages : IPackages
{
#region
public ResponseData Auth(RequestData rData)
{
var data = rData.labelDetail.Split('|'); //Keep getting error here
var response = new ResponseData
{
Name = data[0],
PackageID = data[1]
};
return response;
}
#endregion
}
[DataContract(Namespace = "http://www.labels.com/label")]
public class RequestData
{
[DataMember]
public string labelDetail { get; set; }
}
[DataContract]
public class ResponseData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string PackageID { get; set; }
}
}
答案 0 :(得分:0)
您的元素名称不匹配。在XML请求中,根元素RequestData
具有名为details
的嵌套元素:
string postData = "<?xml version='1.0' encoding='UTF-8'?><RequestData xmlns='http://www.labels.com/label'><details>Test|Second Part</details></RequestData>";
但是在您的数据合同中,相应的属性名为labelDetail
:
[DataContract(Namespace = "http://www.labels.com/label")]
public class RequestData
{
[DataMember] // Notice that "Name" is not set
public string labelDetail { get; set; }
}
您需要使名称匹配,例如:
[DataMember(Name="details")]
public string labelDetail { get; set; }