在WCF REST服务中,通过POST发送参数的正确URI是什么?

在WCF REST服务中,通过POST发送参数的正确URI是什么?,wcf,rest,parameters,uri,Wcf,Rest,Parameters,Uri,假设我在地址“”处指定了以下WCF REST服务 现在,我可以使用地址“”调用Fiddler中的REST服务,它可以正常工作(我将得到一个返回值) 但是,如果我想向REST服务发送参数,该怎么办?我是否应该将接口定义更改为如下所示: [ServiceContract] public interface IMyRESTService { [OperationContract] [WebInvoke( Method = "POST", UriTemplate = "/receive/{tex

假设我在地址“”处指定了以下WCF REST服务

现在,我可以使用地址“”调用Fiddler中的REST服务,它可以正常工作(我将得到一个返回值)

但是,如果我想向REST服务发送参数,该怎么办?我是否应该将接口定义更改为如下所示:

[ServiceContract]
public interface IMyRESTService
{
[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/receive/{text}")]
string Receive(string text);
现在,如果我使用地址“”调用Fiddler中的REST服务,它会工作(它发送参数“mytext”,我会得到一个返回值)。那么,这是通过POST发送参数的正确URI吗

让我困惑的是,当我发送参数时,我不知道如何在代码中准确地使用这个URI。我有下面的代码,它几乎完成了向WCF REST服务发送POST数据的工作,但我一直在研究如何在URI中考虑参数

Dictionary<string, string> postDataDictionary = new Dictionary<string, string>();
      postDataDictionary.Add("text", "mytext");

      string postData = "";
      foreach (KeyValuePair<string, string> kvp in postDataDictionary)
      {
        postData += string.Format("{0}={1}&", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value));
      }
      postData = postData.Remove(postData.Length - 1); 

      Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive");
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
      req.Method = "POST";
      byte[] postArray = Encoding.UTF8.GetBytes(postData);
      req.ContentType = "application/x-www-form-urlencoded";
      req.ContentLength = postArray.Length;

      Stream dataStream = req.GetRequestStream();
      dataStream.Write(postArray, 0, postArray.Length);
      dataStream.Close();

      HttpWebResponse response = (HttpWebResponse)req.GetResponse();
      Stream responseStream = response.GetResponseStream();
      StreamReader reader = new StreamReader(responseStream);

      string responseString = reader.ReadToEnd();

      reader.Close();
      responseStream.Close();
      response.Close();
或者这样做(这是可行的,但没有任何意义,因为参数应该以其他方式添加,而不是直接添加到URI地址)


如果您能帮助我,我很高兴,使用WCF REST服务不会那么困难

因此,如果您希望将原始数据(如XML)发送到WCF REST服务(并返回),下面介绍如何执行此操作。但是我不得不说,在找到这个解决方案之前,我花了很多时间在谷歌上搜索,因为所有的例子都只是在讨论在URI中发送参数(拜托,常见的情况是发送XML,而在URI中你不能正确地做到这一点)。当我最终找到正确的代码示例时,我发现在WCF中这是不够的,因为我得到了错误“400错误请求”。此错误是由以下事实造成的:如果我不使用一些自定义代码覆盖原始XML,WCF将无法使用原始XML(拜托,你是怎么想的Microsoft?),请在下一版本的.NET Framework中修复此问题)。因此,如果做这样一件基本的事情会如此困难和耗时,我一点也不满意)

**IMyRESTService.cs(服务器端代码)**

**客户端代码**

XmlDocument MyXmlDocument = new XmlDocument();
MyXmlDocument.Load(FilePath);
byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml);

Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/Receive");

Request.ContentLength = RequestBytes.Length;

Request.Method = "POST";

Request.ContentType = "text/xml";

Stream RequestStream = Request.GetRequestStream();
RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
RequestStream.Close();

HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string ResponseMessage = reader.ReadToEnd();
response.Close();
**XmlContentTypeMapper.cs(强制WCF接受原始XML的服务器端自定义代码)**

**Web.config(用于使用自定义代码的服务器端配置设置)


使用HTTP POST调用WCF Web服务

我也遇到了同样的问题,使用Fiddler时,每次我尝试使用Body发布时都会收到403(错误请求)错误。我花了几个小时找到的修复方法是将内容类型更改为application/xml,并将正文作为xml发送

这是我在Fiddler的标题中使用的行:

Content-Type: application/xml;charset=UTF-8 
现在,我还没有使用Microsoft.Http库,在我的客户端中,我使用WebRequest进行发布,并将内容类型设置为:

request.ContentType = "application/xml;charset=UTF-8";
对我来说很好

现在,如果您想使用URI发送参数,我建议您使用WebGet而不是带有POST的WebInvoke。

我收到这个(“400”)错误消息,但仅当从运行旧版本(精简版)的.NET的手持设备调用时。至少在我的情况下,我不认为我需要对服务器进行更改,因为我的代码在“当代”客户端项目(VS2013中创建的Winforms应用程序)中运行良好
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
Stream Receive(Stream text);
XmlDocument MyXmlDocument = new XmlDocument();
MyXmlDocument.Load(FilePath);
byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml);

Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/Receive");

Request.ContentLength = RequestBytes.Length;

Request.Method = "POST";

Request.ContentType = "text/xml";

Stream RequestStream = Request.GetRequestStream();
RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
RequestStream.Close();

HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string ResponseMessage = reader.ReadToEnd();
response.Close();
public class XmlContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw;
}
}
<endpoint binding="customBinding" bindingConfiguration="XmlMapper" contract="MyRESTService.IMyRESTService"
           behaviorConfiguration="webHttp"    />

<bindings>
  <customBinding>
    <binding name="XmlMapper">
      <webMessageEncoding webContentTypeMapperType="MyRESTService.XmlContentTypeMapper, MyRESTService"/>
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>
</bindings>
Content-Type: application/xml;charset=UTF-8 
request.ContentType = "application/xml;charset=UTF-8";