2012-11-11 58 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標籤中。

如果您希望在您的服務中接收二進制數據,則應聲明輸入參數爲流,然後不在服務器上完成自動序列化,並且您完全按客戶端發送它的方式接收消息。

相關問題