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

あなたがしたいと思うかもしれないもう一つのことはあなたのためにxmlをシリアル化するすべての大変な仕事をするためにClientBaseを拡張することです。特に、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 に答える