【问题标题】:Invoking a WCF WebInvoke method with HttpWebRequest POST and mapping parameters correctly使用 HttpWebRequest POST 调用 WCF WebInvoke 方法并正确映射参数
【发布时间】:2011-11-17 21:57:34
【问题描述】:

我已成功将以下 WCF 合同设置为服务:

[WebInvoke(Method = "POST", UriTemplate = "submit/{apiKey}", ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
DataServiceResult Submit(string apiKey, string email);

我正在尝试使用 HttpWebRequest 调用它

string url = "http://localhost:51462/Api.svc/web/submit/key";
string formatString = "email=hello@there.com";

byte[] postData = Encoding.ASCII.GetBytes(formatString);

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.Referer = url;
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;

System.IO.Stream outputStream = request.GetRequestStream();
request.AllowAutoRedirect = true;
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string srcString = reader.ReadToEnd();

当我运行此代码时,apikey 已成功映射。问题是我不确定如何将电子邮件参数映射到当前为空的提交方法。

另一个注意事项,在我的应用程序中,我将 10-15 个参数作为字符串传递,因此向 UriTemplate 添加电子邮件不是我要寻找的答案。

【问题讨论】:

    标签: wcf


    【解决方案1】:

    只是检查一下,为什么在 URL 中传递对象(XML 或 JSON)不是一个选项?

    您可以看看这个post 以供参考。如果我弄错了,请告诉我:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2012-07-10
      • 2011-07-13
      相关资源
      最近更新 更多