使用HttpWebRequest向WCF服务发布请求
我的服务合同使用HttpWebRequest向WCF服务发布请求,wcf,Wcf,我的服务合同 [ServiceContract] public interface ITsdxService { [OperationContract] [WebGet(UriTemplate="/GetTestCostCentre")] CostCentre GetTestCostCentre(); [OperationContract] [WebInvoke(UriTemplate="/SetCostCentre", Method="POST")]
[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)请求错误
我做错了什么?像这样更改您的客户端代码
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服务代理。您可能想做的另一件事是扩展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>
为什么不在VisualStudio中使用AddServiceReference并使用生成的代理。我这样问是因为你在客户端使用C#…@Beygi我们的客户端将使用Java来实现这一点,我只想自己测试这项服务。哇,非常感谢!!现在我发现问题在于我没有添加request.ContentType=“application/xml”代码>字符串。