假设我已经在地址“http://localhost/MyRESTService/MyRESTService.svc”中指定了以下WCF REST服务
[ServiceContract]
public interface IMyRESTService
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/receive")]
string Receive(string text);
现在,我可以使用地址“http://localhost/MyRESTService/MyRESTService.svc/receive”在Fiddler中调用我的REST服务,它可以工作(我将获得一个返回值)。
但是,如果我想将参数发送到REST服务怎么办?我是否应该将接口定义更改为以下形式:
[ServiceContract]
public interface IMyRESTService
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/receive/{text}")]
string Receive(string text);
现在,如果我使用地址“http://localhost/MyRESTService/MyRESTService.svc/receive/mytext”在Fiddler中调用REST服务,它将起作用(它将发送参数“mytext”,并且我将获得一个返回值)。那么这是通过POST发送参数的正确URI吗?
使我感到困惑的是,我在发送参数的同时不知道如何在代码中完全使用此URI。我有以下这段代码,几乎可以完成将POST数据发送到WCF REST服务的操作,但是我仍然对如何使用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代码应为
这个
Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive");
或这样(这可行,但是没有任何意义,因为应该以其他方式而不是直接将参数添加到URI地址中)
Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/receive/mytext");
我很高兴能为您提供帮助,使用WCF REST Services并不难。
最佳答案
因此,如果要将原始数据(例如XML)发送到WCF REST服务(并返回),请按照以下步骤操作。但是我不得不说,在找到此解决方案之前,我花了很多时间进行谷歌搜索,因为所有示例都只是在谈论在URI中发送参数(来吧,常见的情况是发送XML,您不能正确地做到这一点。在URI中)。最后,当我找到正确的代码示例时,出现了错误消息“400 Bad Request”,这在WCF中还远远不够。该错误是由以下事实引起的:如果我不通过使用一些自定义代码覆盖它来强制WCF不能使用我的原始XML(请问,您在想什么Microsoft?请在下一版.NET中对此进行修复。框架)。所以我根本不满足于做这样一个基本的事情会如此艰辛和耗时。
** IMyRESTService.cs(服务器端代码)**
[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();
** XmlContentTypeMapper.cs(强制WCF接受原始XML的服务器端自定义代码)**
public class XmlContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw;
}
}
** Web.config(用于利用自定义代码的服务器端配置设置)
<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://social.msdn.microsoft.com/forums/en-us/wcf/thread/4074F4C5-16CC-470C-9546-A6FB79C998FC