【问题标题】:Bad request 400 on sending xml request in wcf rest在 wcf rest 中发送 xml 请求时出现错误请求 400
【发布时间】:2011-01-10 17:54:51
【问题描述】:

我正在编写一个使用 wcf rest 进行身份验证的示例应用程序。这是代码的快照:

服务接口:

[ServiceContract]
public interface IAuthenticate
{
    [OperationContract]
    [WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, 
Method = "POST", UriTemplate = "/VUser",RequestFormat= WebMessageFormat.Xml ), ]
    string CreateUser(VUser user);
}

数据合约类:

[DataContract]
public class VUser
{
    public VUser()
    {
    }

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

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

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

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

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

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

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

    [DataMember]
    public int CountryCode { get; set; }
}

服务类:

public class Authenticate : IAuthenticate
{

    #region IAuthenticate members
    public string CreateUser(Vuser user)
    {
        //processing xml for response

    }
    #endregion IAuthenticate
}

客户端代码:

       Uri baseAddress = new Uri("http://localhost:8000");

        using (WebServiceHost host = new WebServiceHost(typeof(Authenticate), baseAddress))
        {
            host.Open();
            Console.WriteLine("Press any key to terminate");
            Console.ReadLine();
            host.Close();

        }

现在我使用 fiddler 在 host.open() 之后发送请求并发送请求显示:

发帖http://localhost:8000/Vuser/

用户代理:提琴手 主机:本地主机:8000 内容长度:233 内容类型:文本/xml

在请求正文中:

沙 r 苏尼尔 苏尼尔 919900101948 冬天 男性 01

但它返回给我 HTTP/1.1 400 错误请求。我的问题是我是否将 vuser 类正确传递给 create user 方法,或者是否有任何其他方式来发送 vuser。

请帮帮我。

【问题讨论】:

标签: wcf rest


【解决方案1】:

这可能是序列化的问题。

序列化使用默认构造函数,不带参数。

在 C# 中,编译器会自动创建一个默认构造函数,除了如果你创建一个带参数的构造函数。

Authenticate 类缺少默认构造函数,因此您将有问题通过 WCF 发送它。

【讨论】:

  • hi shiraz 我也创建了一个默认构造函数,但它仍然是同样的错误......
【解决方案2】:

请在 DataContract 类中指定 Datacontract 命名空间

[DataContract(Namespace = "http://xxx.xxx.xxx/Service.svc")]

并在 Xml 文件中遵循相同的操作

【讨论】:

    【解决方案3】:

    客户端和服务器中的命名空间应该匹配。尝试将命名空间名称添加为

    [DataContract(Namespace = "http://sample.com")]
    public class VUser
    

    在服务器合同中。然后确保 xml 字符串具有相同命名空间的 xmlns 值

    "<VUser xmlns=\"http://sample.com" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">...</VUser>"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多