2012-06-24 85 views
2

我的服務合同的POST請求WCF服務通過使用HttpWebRequest的

[ServiceContract] 
public interface ITsdxService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate="/GetTestCostCentre")] 
    CostCentre GetTestCostCentre(); 

    [OperationContract] 
    [WebInvoke(UriTemplate="/SetCostCentre", Method="POST")] 
    string SetCostCentre(CostCentre cc); 
} 

public class TsdxService : ITsdxService 
{ 
    public CostCentre GetTestCostCentre() 
    { 
     CostCentre cc = new CostCentre(); 
     cc.Code = "Test"; 
     cc.Name = "Test Cost Centre"; 
     cc.Description = new byte[] { 12, 34, 89, 240, 66, 87, 189 }; 
     cc.SAPStatus = "Existent"; 
     cc.SAPSiteFolder = "Folder1"; 
     return cc; 
    } 

    public string SetCostCentre(CostCentre cc) 
    { 
     return cc.Code; 
    } 
} 

然後我啓動該服務,並嘗試從不同的應用與它的工作:

Uri requestUri = new Uri(textBox1.Text + "/tsdx/GetTestCostCentre"); 

HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest; 
XElement root; 

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
{ 
    StreamReader reader = new StreamReader(response.GetResponseStream()); 
    root = XElement.Parse(reader.ReadToEnd()); 
    textBox2.Text = root.ToString(); 
} 

一切正常,我正在獲取xml文檔。但我面對的問題,當我嘗試發送POST請求該服務:

Uri requestUri = new Uri(textBox1.Text + "/tsdx/SetCostCentre"); 

HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest; 

byte[] bytes = Encoding.UTF8.GetBytes(textBox2.Text); 

request.ContentLength = bytes.Length; 
request.Method = "POST"; 

Stream dataStream = request.GetRequestStream(); 
dataStream.Write(bytes, 0, bytes.Length); 
dataStream.Close(); 

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
{ 
    StreamReader reader = new StreamReader(response.GetResponseStream()); 
    textBox2.Text = reader.ReadToEnd(); 
} 

例外:遠程服務器返回錯誤:(400)錯誤的請求。

我做錯了什麼?

+0

究竟爲什麼你不使用的VisualStudio中的AddServiceReference並使用生成的代理。我問這是因爲你使用C#作爲客戶端... – Beygi

+0

@Beygi我們的客戶端將使用Java,我只是想自己測試這個服務。 –

回答

2

更改您的客戶端代碼這樣

byte[] bytes = Encoding.UTF8.GetBytes(@"<CostCentre xmlns=""http://schemas.datacontract.org/2004/07/WCF_BadRequestService""> 
                 <Code>String content</Code> 
                 <Description>QmFzZSA2NCBTdHJlYW0=</Description> 
                 <Name>String content</Name> 
                 <SAPSiteFolder>String content</SAPSiteFolder> 
                 <SAPStatus>String content</SAPStatus> 
                </CostCentre>"); 

request.ContentLength = bytes.Length; 
request.Method = "POST"; 
request.ContentType = "application/xml"; 

現在它的罰款。

而且我認爲Java支持WCF BasicHttpBinding,並且您可以使用Java提供的工具使用WCF服務以簡單支持的方式生成Web服務代理。

+0

哇,非常非常感謝!現在我發現問題在於我不添加'request.ContentType =「application/xml」;'string。 –

+0

@DonTomato我非常樂意幫助你... – Beygi

0

你可能想要做的另一件事是擴展ClientBase爲你做序列化xml所做的所有努力。特別是如果你想支持多種消息格式,比如json,它會讓你的生活變得更容易,再加上它會改變接口編譯時錯誤而不是運行時錯誤。

public class ITsdxServiceProxy : ClientBase<ITsdxService>, ITsdxService { 

    #region ITsdxService Members 

    public CostCentre GetTestCostCentre() { 
     return Channel.GetTestCostCentre(); 
    } 

    public string SetCostCentre(CostCentre cc) { 
     return Channel.SetCostCentre(cc); 
    } 

    #endregion 
} 

在客戶端使用

var proxy = new ITsdxServiceProxy(); 
var costCenter = proxy.GetTestCostCentre(); 

配置在客戶端

<system.serviceModel> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="web"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<client> 

    <endpoint 
      address="Root address for rest service" 
      binding="webHttpBinding" 
      behaviorConfiguration="web" 
      contract="FullyQualifiedNameOfInterface.ITsdxService"> 
    </endpoint>  

</client> 
</system.serviceModel>