WCF REST POST XML - 远程服务器返回错误:(400)错误请求

时间:2009-05-26 17:40:32

标签: wcf rest

我已使用WCF服务库公开了我的REST API服务,并由控制台应用程序启动。当我尝试从客户端(另一个控制台应用程序)使用该服务时,我收到“错误请求”异常。

请参阅以下代码: -

[ServiceContract(Namespace = "http://www.test.com/Enrollment")]
public interface IEnrollService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "enroll", 
           RequestFormat = WebMessageFormat.Xml, 
           ResponseFormat = WebMessageFormat.Xml, 
           BodyStyle= WebMessageBodyStyle.Bare)]
    string EnrollData(Enroll enrData);
}

public class EnrollService : IEnrollService
{
    public string EnrollData(Enroll enrData)
    {
        return "Hello, your test data is " + enrData.AccountNumber; 
    }
}

DataContract

[DataContract(Namespace = "http://www.test.com/Enrollment")]
public class Enroll
{

    [DataMember]
    public string ClientId
    {
        get;
        set;
    }

    [DataMember]
    public string AccountNumber
    {
        get; 
        set; 
    }

    [DataMember]
    public string TaxId
    {
        get; 
        set; 
    }

    [DataMember]
    public string TaxType
    {
        get; 
        set; 
    }

    [DataMember]
    public string EmailAddress
    {
        get; 
        set; 
    }

    [DataMember]
    public string Pin
    {
        get; 
        set; 
    }

    [DataMember]
    public string State
    {
        get; 
        set; 
    }

}

app.config设置

<system.serviceModel>
  <services>
    <service name="OEA.REST.EnrollService" behaviorConfiguration="OEABehavior">
      <endpoint address="http://localhost:8010/OEA/Service/EnrollService" 
                binding="webHttpBinding" behaviorConfiguration="webHttp"
                contract="OEA.REST.IEnrollService" />
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8010/OEA/Service/EnrollService" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttp">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="OEABehavior">
        <serviceMetadata httpGetEnabled="True" />
        <serviceDebug includeExceptionDetailInFaults="True" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

客户端代码:

HttpWebRequest req = null;
HttpWebResponse res = null;
try
{
    string url = "http://localhost:8010/OEA/Service/EnrollService/enroll";
    req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentType = "application/xml; charset=utf-8";
    req.Timeout = 30000;
    req.Headers.Add("SOAPAction", url);              

    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
    xmlDoc.XmlResolver = null;
    xmlDoc.Load(@"d:\test.xml");
    string sXML = xmlDoc.InnerXml;
    req.ContentLength = sXML.Length;
    System.IO.StreamWriter sw = new System.IO.StreamWriter(req.GetRequestStream());
    sw.Write(sXML);
    sw.Close();

    res = (HttpWebResponse)req.GetResponse();
}
catch (Exception ex)
{
    System.Console.WriteLine(ex.Message);
}

test.xml的内容

<Enroll>
    <ClientId>000</ClientId>
    <AccountNumber>123</AccountNumber>
    <TaxId>123</TaxId>
    <TaxType>123</TaxType>
    <EmailAddress>123</EmailAddress>
    <Pin>123</Pin>
    <State>122</State>
</Enroll>

我在req.GetResponse()方法调用中遇到“Bad Request”异常。刚开始学习REST。我对POST请求感到困惑。任何帮助将受到高度赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:12)

您的POST有效负载应为:

<Enroll xmlns="http://www.test.com/Enrollment">
<ClientId>000</ClientId>
<AccountNumber>123</AccountNumber>
<TaxId>123</TaxId>
<TaxType>123</TaxType>
<EmailAddress>123</EmailAddress>
<Pin>123</Pin>
<State>122</State>
</Enroll>

答案 1 :(得分:7)

您的XML命名空间是错误的。我没有时间为您纠正,但需要根据您的数据合同在“http://www.test.com/Enrollment”命名空间中。