.net 来自WCF web调用的错误请求
我所有的GET端点都像champ一样工作,但我正在尝试实现一个webinvoke方法=“POST” 我想我的格式有问题,但我不知道是什么,有人能帮忙吗.net 来自WCF web调用的错误请求,.net,wcf,c#-4.0,webinvoke,.net,Wcf,C# 4.0,Webinvoke,我所有的GET端点都像champ一样工作,但我正在尝试实现一个webinvoke方法=“POST” 我想我的格式有问题,但我不知道是什么,有人能帮忙吗 [ServiceContract] interface iFlowRate { [OperationContract] [WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}",RequestFormat= WebMessageFormat.Xml)]
[ServiceContract]
interface iFlowRate
{
[OperationContract]
[WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}",RequestFormat= WebMessageFormat.Xml)]
string AddFlowRate(string apikey,FlowRate flowrate);
}
当我调试这个时,它甚至没有进入这个方法。
我这样打电话给服务部
string postData = "<FlowRate ><wellname>wellname</wellname></FlowRate>";
//Setup the http request.
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentLength = postData.Length;
request.ContentType = "application/xml";
request.KeepAlive = true;
StreamWriter streamwriter = new
StreamWriter(request.GetRequestStream());
streamwriter.Write(postData);
streamwriter.Close();
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Read the response
StreamReader responsereader = new StreamReader(response.GetResponseStream());
string strResponseData = responsereader.ReadToEnd();
string postData=“wellname”;
//设置http请求。
HttpWebRequest-request=WebRequest.Create(url)为HttpWebRequest;
request.Method=“POST”;
request.ContentLength=postData.Length;
request.ContentType=“应用程序/xml”;
request.KeepAlive=true;
StreamWriter StreamWriter=新建
StreamWriter(request.GetRequestStream());
streamwriter.Write(postData);
streamwriter.Close();
//得到回应。
HttpWebResponse=(HttpWebResponse)request.GetResponse();
//阅读回复
StreamReader responsereader=新的StreamReader(response.GetResponseStream());
string strresponsea=responsereader.ReadToEnd();
有什么想法吗?顺便说一句,使用WCF 4.0,任何帮助都是非常感谢的。这让我非常痛苦,直到不久前我终于找到了答案 以下是我调查结果的来源: 但我会切入正题 首先,设置ServiceContract的名称空间
[ServiceContract(Namespace = "http://mytagservice")]
现在,我确信还有另一种方法可以让它工作,但这就是我破解它的方式。将BodyStyle设置为“包裹”。默认的请求格式是XML,因此如果不想在此处设置,则无需在此处进行设置
[WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}", BodyStyle = WebMessageBodyStyle.Wrapped)]
然后将xml更改为包含包装器和名称空间。特别注意标记名,因为它们区分大小写
string postData = "<AddFlowRate xmlns='http://mytagservice'><flowrate><wellname>wellname</wellname></flowrate></AddFlowRate>";
string postData=“wellname”;
因为它使用的是包装消息类型,所以此解决方案将适用于您想要的任意多个参数。直到不久前,我终于找到了答案,这才让我彻底崩溃 以下是我调查结果的来源: 但我会切入正题 首先,设置ServiceContract的名称空间
[ServiceContract(Namespace = "http://mytagservice")]
现在,我确信还有另一种方法可以让它工作,但这就是我破解它的方式。将BodyStyle设置为“包裹”。默认的请求格式是XML,因此如果不想在此处设置,则无需在此处进行设置
[WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}", BodyStyle = WebMessageBodyStyle.Wrapped)]
然后将xml更改为包含包装器和名称空间。特别注意标记名,因为它们区分大小写
string postData = "<AddFlowRate xmlns='http://mytagservice'><flowrate><wellname>wellname</wellname></flowrate></AddFlowRate>";
string postData=“wellname”;
因为它使用的是包装消息类型,所以此解决方案将适用于您想要的任意多个参数。您尝试发布的URL是什么?另外,流量类是如何定义的?URL=localhost:4399/FlowRate/Add?apikey=32q13e4-c78a-ce9d-e011-15eacd8e8958“{[DataContract]公共类流量{[DataMember]公共字符串wellname{get;set;}}}}您尝试发布的URL是什么?另外,如何定义流量类?URL=localhost:4399/FlowRate/Add?apikey=32q13e4-c78a-ce9d-e011-15eacd8e8958”;{[DataContract]公共类流量{[DataMember]公共字符串井名{get;set;}}}