2

我的服务合同

[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)错误请求。

我做错了什么?

4

2 回答 2

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服务代理。

于 2012-06-24T12:48:39.620 回答
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>
于 2012-06-24T21:39:18.467 回答