我正在尝试设置一个简单的服务,允许用户在POST主体中将Pickup对象作为XML传入。如果我将服务设置为接收Stream类型,我可以让它工作正常,但我不知道我需要更改为了它而不是自定义类型是可行的。当我告诉服务器期望提货类型时,我收到“请求错误。服务器遇到......”
我有人可以告诉我在服务器/客户端需要更改以使其工作我会非常感激。
我的数据合同
[DataContract]
public class Pickup
{
[DataMember]
public string DelZip
{ get; set; }
}
运营合同
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/pickups",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
string RequestPickup(Pickup pickup);
public string RequestPickup(Pickup pu)
{
return pu.DelZip;
}
配置
<services>
<service name="TestAPI.Services.TestServices" >
<endpoint address=""
binding="webHttpBinding" behaviorConfiguration="webHttp"
contract="TestAPI.Services.ITestServices" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
我的客户代码
const string url = "http://localhost:18463/TestServices.svc/pickups";
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/xml";
var xmlDoc = new XmlDocument { XmlResolver = null };
xmlDoc.LoadXml(@"<Pickup><DelZip>55555</DelZip></Pickup>");
string sXml = xmlDoc.InnerXml;
req.ContentLength = sXml.Length;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write(sXml);
sw.Close();
res = (HttpWebResponse)req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);