【发布时间】:2011-07-11 03:22:44
【问题描述】:
我有一个简单的安静的 wcf 服务,我创建它只是为了好玩。我尝试了调用 post 方法,但我失败了。这是摘要;
这是我的服务合约接口;
namespace WcfServiceWithNortwind.Smooth {
[ ServiceContract]
public interface INorthwindService {
[ WebGet (UriTemplate = "/" )]
[ OperationContract ]
List <Category2 > GetCategories();
[ WebGet (UriTemplate = "categories/{id}" )]
[ OperationContract ]
Category2 GetCategory(string id);
[ WebInvoke (UriTemplate = "categories/{id}" , Method = "DELETE")]
[ OperationContract ]
void DeleteCategory(string id);
[ WebInvoke (UriTemplate = "categories" , Method = "POST")]
void AddCategory(Category2 category);
}
}
这是我的服务的数据成员,它是 Category2 类;
namespace WcfServiceWithNortwind.Smooth {
[ DataContract]
public class Category2 {
[ DataMember ]
public int CategoryID { get; set ; }
[ DataMember ]
public string CategoryName { get; set ; }
[ DataMember ]
public string Description { get; set ; }
}
}
这是我试图调用 post 方法的代码;
System.Xml. XmlDocument doc = new System.Xml. XmlDocument();
doc.Load(context.Server.MapPath( "~/@xml/category.xml" ));
string strHostAddress = "http://localhost:54860/Smooth/Nortwind.svc/categories" ;
string xmldata = doc.OuterXml;
string _data = String .Format( "{0}{1}", "category=" , xmldata);
WebRequest _WebRequest = WebRequest .Create(strHostAddress);
_WebRequest.Method = "POST" ;
byte [] byteArray = Encoding .UTF8.GetBytes(_data);
_WebRequest.ContentType = "application/x-www-form-urlencoded" ;
_WebRequest.ContentLength = byteArray.Length;
Stream dataStream = _WebRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
var _response = _WebRequest.GetResponse();
这是我用来发送的xml文件,即category.xml;
<? xml version =" 1.0 "?>
<Category2>
<CategoryID />
<CategoryName> Tugberk's Category </CategoryName>
<Description> .net, wcf, wpf, mvc, silverlight </Description>
</Category2>
当我运行代码时,我一尝试调用 _WebRequest.GetResponse() 就会收到以下错误;
传入的消息有一个 意外消息格式“原始”。这 预期的消息格式 操作是'Xml'; '杰森'。这个可以 是因为 WebContentTypeMapper 有 没有在绑定上配置。 参见文档 WebContentTypeMapper 了解更多详情。
我也尝试使用带有请求生成器功能的提琴手发布它,我也遇到了同样的错误。
那么我在这里想念什么?
【问题讨论】:
标签: wcf rest http-post webrequest wcf-rest