2012-11-11 54 views
0

我想将对象发布到使用REST和XML的WCF服务,但我一直在收到"error: (400) Bad Request"引发。我知道这个网站上有很多关于同一问题的帖子,但我似乎无法找到解决方案。在C#中通过REST XML发布对象WCF返回400错误的请求

我的WCF服务代码:

IPhoneBookService.cs:

[OperationContract] 
    [WebInvoke(UriTemplate = "/addentry/", 
       Method = "POST", 
       BodyStyle = WebMessageBodyStyle.Bare, 
       RequestFormat = WebMessageFormat.Xml, 
       ResponseFormat = WebMessageFormat.Xml)] 
    void AddEntry(Contact contact); 

PhoneBookService.cs:

DataPhonebookDataContext dc = new DataPhonebookDataContext(); 
    public void AddEntry(Contact contact) 
    { 
     if (contact == null) 
     { 
      throw new ArgumentNullException("contact"); 
     } 
     dc.Contacts.InsertOnSubmit(contact); 

     try 
     { 
      dc.SubmitChanges(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     }   

    } 

客户端(ASP页)代码:

private WebClient client = new WebClient(); 
    HttpWebRequest req; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     req = (HttpWebRequest)WebRequest.Create("http://localhost:1853/PhoneBookService.svc/addentry/"); 
     req.Method = "POST"; 
     req.ContentType = "application/xml; charset=utf-8"; 
     req.Timeout = 30000; 
     client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); 
    } 
    protected void btnAddEntry_Click(object sender, EventArgs e) 
    { 
     var contact = new Contact(); //Contact class from WCF service reference 
     contact.LastName = txtLastName.Text; 
     contact.FirstName = txtFirstName.Text; 
     contact.PhoneNumber = txtPhone.Text; 

     //Code using Webclient 
     //client.UploadData(new Uri("http://localhost:1853/PhoneBookService.svc/addentry/"), TToByteArray<Contact>(contact)); 

     //Code using webrequest 
     byte[] buffer = TToByteArray<Contact>(contact); 
     req.ContentLength = buffer.Length; 
     Stream PostData = req.GetRequestStream(); 
     PostData.Write(buffer, 0, buffer.Length); 
     PostData.Close(); 

     HttpWebResponse resp = req.GetResponse() as HttpWebResponse; 
     Encoding enc = System.Text.Encoding.GetEncoding(1252); 
     StreamReader loResponseStream = 
     new StreamReader(resp.GetResponseStream(), enc); 
     string response = loResponseStream.ReadToEnd(); 
    } 
    private byte[] TToByteArray<T>(T item) 
    { 
     BinaryFormatter bf = new BinaryFormatter(); 
     MemoryStream ms = new MemoryStream(); 

     if (!typeof(T).IsSerializable) 
     { 
      throw new ArgumentException("The type of the argument must be serializable"); 
     } 
     bf.Serialize(ms, item); 
     return ms.ToArray(); 
    } 

Contact类在DataContext定义,由LinqToSQL类生成女巫。我编辑Contact类是可序列化的。

+0

您能否指出代码中“错误请求”异常的确切位置? – tempidope

+0

如果您创建一个侦听XML格式邮件请求的Web Service,它只会响应XML格式的请求。你用二进制格式化的数据提供它。我认为你的问题。 –

回答

1

您创建了一个Web服务来侦听XML发布请求。这意味着请求消息格式必须是XML。

另一方面,您的客户以二进制格式序列化合约。尝试使用XMLSerializer,而不是BinaryFormatter。

WebMessageBodyStyle.Bare属性不表示二进制数据。它只表明该消息未包含在具有元信息的附加XML标签中。

如果您希望在您的服务中接收二进制数据,则应声明输入参数为流,然后不在服务器上完成自动序列化,并且您完全按客户端发送它的方式接收消息。

相关问题