通过使用HttpWebRequest对WCF服务的POST请求

时间:2012-06-24 10:37:11

标签: wcf

我的服务合同

[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)Bad Request。

我做错了什么?

2 个答案:

答案 0 :(得分: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服务代理。

答案 1 :(得分: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>