我正在用C#开发一个Restful服务,在我使用
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle =
WebMessageBodyStyle.Wrapped, UriTemplate = "json/?id={id}")]
string jdata(string id);我的相应函数实现是:
public string json(string id)
{
return "You Typed : "+id;
}到目前为止,一切都运行得很好,但是当我更改WenInvoke Method="POST“时,我不得不面对一个”不允许的方法“。
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle =
WebMessageBodyStyle.Wrapped, UriTemplate = "json/?id={id}")]
string jdata(string id);发布于 2014-12-04 18:40:14
你得到“方法不允许”,因为你通过get而不是POST访问Uri "json/?id={id}“。与您的客户检查这一点(您没有提到如何调用此资源)。请给出一些进一步的细节,您是如何尝试在客户端使用您的web服务。.Net是客户端吗?
为了测试你的API,我推荐使用Fiddler -当你可以在发送http请求之前显式地指定是使用GET还是POST:

另一件事是,您可能无意中使用了"json“作为Uri,但却将ResponseFormat定义为WebMessageFormat.Xml。这对客户来说是不是有点令人困惑?也许你想把JSON还回去?在这种情况下,我建议在请求和响应中都使用Json:
[WebInvoke(Method = "POST", UriTemplate = "/ValidateUser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]发布于 2013-10-21 04:49:23
[OperationContract]
[WebInvoke(Method="POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "json")]
string jdata(string id);这是你的合同应该是什么样子,然后在客户端
WebRequest httpWebRequest =
WebRequest.Create(
url);
httpWebRequest.Method = "POST";
string json = "{\"id\":\"1234"\}"
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
httpWebRequest.Timeout = 1000000;
WebResponse webrespon = (WebResponse)httpWebRequest.GetResponse();
StreamReader stream = new StreamReader(webrespon.GetResponseStream());
string result = stream.ReadToEnd();
Console.Out.WriteLine(result);上面只是我用来测试我的服务的东西。希望能有所帮助。
https://stackoverflow.com/questions/18918199
复制相似问题