在WCF REST Services中通过POST发送参数的正确URI是什么? [英] What is the correct URI for sending parameters via POST in WCF REST Services?

查看:66
本文介绍了在WCF REST Services中通过POST发送参数的正确URI是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已在地址" http://localhost/MyRESTService/MyRESTService.svc "

[ServiceContract]
public interface IMyRESTService
{
[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/receive")]
string Receive(string text);

现在,我可以使用地址" http://localhost/MyRESTService/在Fiddler中调用我的REST服务. MyRESTService.svc/receive ",它可以正常工作(我将获得一个返回值).

Now I can call my REST service in Fiddler using the address "http://localhost/MyRESTService/MyRESTService.svc/receive" and it works (I'll get a return value).

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

But what if I want to send parameters to my REST Service? Should I change my interface definition to look like this:

[ServiceContract]
public interface IMyRESTService
{
[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/receive/{text}")]
string Receive(string text);

现在,如果我将使用地址" http://在Fiddler中调用REST服务localhost/MyRESTService/MyRESTService.svc/receive/mytext "(它发送参数"mytext",我将得到一个返回值).那么这是通过POST发送参数的正确URI吗?

Now if I'll call the REST Service in Fiddler using the address "http://localhost/MyRESTService/MyRESTService.svc/receive/mytext" it works (it sends the parameter "mytext" and I'll get a return value). So is this the correct URI for sending parameters via POST?

让我感到困惑的是,我不知道如何在发送参数的同时准确地在代码中使用此URI .我有下面的这段代码,几乎可以完成将POST数据发送到WCF REST服务的操作,但是我对如何使用URI考虑参数感到困惑.

What confuses me is that I don't know how to use this URI exactly in code at the same time when I'm sending parameters. I have this following code which is almost complete for sending POST data to a WCF REST Service but I'm in stuck with how to take parameters into account with 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();

如果我想通过POST在代码中发送参数(例如"mytext"),则URI代码要么是

If I'll want to send parameters (e.g. "mytext") in code via POST should the URI code be either

这个

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

或这样(这可行,但是没有任何意义,因为应该以其他方式添加参数,而不是直接将其添加到URI地址中)

or this (this works but it doesn't make any sense since parameters should be added other way and not directly to the URI address)

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

如果您能帮助我,我感到很高兴,使用WCF REST Services并不是那么困难.

I'm glad if you can help me, it can't be so difficult with WCF REST Services.

推荐答案

因此,如果要将XML之类的原始数据发送到WCF REST服务(并返回),请按以下步骤操作.但是我不得不说,在找到此解决方案之前,我花了很多时间进行谷歌搜索,因为所有示例都只是在谈论在URI中发送参数(来吧,常见的情况是发送XML,您不能正确地做到这一点.在URI中).最后,当我找到正确的代码示例时,出现了错误消息"400 Bad Request",这在WCF中还远远不够.此错误是由以下事实引起的:如果我不通过使用一些自定义代码覆盖它来强制WCF,则WCF将无法使用我的原始XML(请问,您在想什么Microsoft?请在下一版.NET中对此进行修复.框架).所以我根本不满足于做这样一个基本的事情可能会如此艰辛和耗时).

So if you want to send raw data such as XML to your WCF REST Service (and also return), here is how to do it. But I have to say that before I found this solution I spend a lot of time googling frustrated as all examples were just talking about sending parameters in the URI (come on, the common scenario is to send XML and you can't do that properly in the URI). And when finally I found the right code examples it came up that it wasn't enough in WCF as I got the error "400 Bad Request". This error was caused by the fact that WCF can't use my raw XML if I don't force it by overriding it with some custom code (come on, what were you thinking Microsoft?, fix this in the next version of .NET Framework). So I'm not satisfied at all if doing such a basic thing can be so hard and time consuming).

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

** IMyRESTService.cs (server-side code) **

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
Stream Receive(Stream text);

**客户端代码**

** client-side code **

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的服务器端自定义代码)**

** XmlContentTypeMapper.cs (server-side custom code which forces WCF to accept raw XML) **

public class XmlContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw;
}
}

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

** Web.config (server-side configuration settings for utilizing the custom code)

<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>

使用HTTP POST调用WCF Web服务 http://社交. msdn.microsoft.com/forums/zh-CN/wcf/thread/4074F4C5-16CC-470C-9546-A6FB79C998FC

Invoke a WCF Web Service with an HTTP POST http://social.msdn.microsoft.com/forums/en-us/wcf/thread/4074F4C5-16CC-470C-9546-A6FB79C998FC

这篇关于在WCF REST Services中通过POST发送参数的正确URI是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆