【问题标题】:bad request from WCF web invoke来自 WCF Web 调用的错误请求
【发布时间】:2011-12-28 13:40:44
【问题描述】:

我所有的 GET 端点都像冠军一样工作,但我正在尝试实现 webinvoke method="POST"。

我认为我的格式有问题,但我不知道是什么问题,有人可以帮忙吗?

[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();

有什么想法吗?顺便说一句,使用 WCF 4.0,非常感谢任何帮助。

【问题讨论】:

  • 您尝试 POST 的 URL 是什么?
  • 另外,FlowRate 类是如何定义的?
  • url = localhost:4369/FlowRate/Add?apikey=32q13e4-c78a-ce9d-e011-15eacd8e8958"; { [DataContract] public class FlowRate { [DataMember] public string wellname { get; set; } } }

标签: .net wcf c#-4.0 webinvoke


【解决方案1】:

直到不久前我终于偶然发现了答案。

这是我的发现的来源:Wrapped BodyStyle in WCF Rest

但我会切入好东西。

首先,设置 ServiceContract 的命名空间。

[ServiceContract(Namespace = "http://mytagservice")]

现在,我确信还有另一种方法可以让它工作,但这就是我破解它的方式。将 BodyStyle 设置为 Wrapped。默认的请求格式是XML,如果你不想,这里不需要设置。

 [WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}", BodyStyle = WebMessageBodyStyle.Wrapped)]

然后更改您的 xml 以包含包装器和命名空间。请特别注意标签名称,因为它们区分大小写。

string postData = "<AddFlowRate xmlns='http://mytagservice'><flowrate><wellname>wellname</wellname></flowrate></AddFlowRate>";

因为它使用包装的消息类型,所以这个解决方案可以处理尽可能多的参数。

【讨论】:

    猜你喜欢
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2014-03-04
    • 2013-06-22
    • 2020-06-21
    相关资源
    最近更新 更多