WCF REST的POST时候报400错误是怎么回事

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WCF REST的POST时候报400错误是怎么回事相关的知识,希望对你有一定的参考价值。

参考技术A Get方法可以正常返回值,POST方法提示:远程服务器返回错误: (400) 错误的请求。
服务端代码如下:
[ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class RestService [WebInvoke(UriTemplate = "Create", Method = "POST")] public string Create(string s) return s; [WebGet(UriTemplate = "Get?id=id",RequestFormat = WebMessageFormat.Json)] public SampleItem Get(string id) return new SampleItem Id = 1, StringValue = "http://s.yanghao.org/program/test";

客户端调用代码如下:
WebClient webClient = new WebClient();webClient.Encoding = System.Text.Encoding.UTF8;webClient.Headers[HttpRequestHeader.ContentType] = "application/json";string tt = webClient.DownloadString("http://localhost:7000/RestService/Get?id=tt");Console.WriteLine(tt);WebClient client=new WebClient();client.Encoding = System.Text.Encoding.UTF8;client.Headers[HttpRequestHeader.ContentType] = "application/json";//下一行报错client.UploadString(new Uri("http://localhost:7000/RestService/Create", UriKind.Absolute), "POST", "test");
次浏览本回答被提问者和网友采纳
参考技术B 用json格式的时候,包体前后加上双引号就可以了。

Post 请求WCF接口报:远程服务器返回错误: (415)

详细错误信息如下

远程服务器返回错误: (415) Cannot process the message because the
content type 'application/x-www-form-urlencoded' was not the expected
type 'text/xml; charset=utf-8'.。

参考技术A <%@ ServiceHost Language="C#" Debug="true"
Service="YourNamespace.YourServiceClass"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
参考技术B 把Cotent-Type的属性从application/x-www-form-urlencoded改为text/xml; charset=utf-8 参考技术C 翻译:由于该文件正被另一进程所占用,导致现在(要进行的程序)无法使用该文件.
就好比你在播放一部电影的同时你想去删掉那个电影文件,一般情况下是删不掉的,就会提示您这么一句类似的话.

以上是关于WCF REST的POST时候报400错误是怎么回事的主要内容,如果未能解决你的问题,请参考以下文章

在 wcf rest 中发送 xml 请求时出现错误请求 400

WCF Webinvoke POST 给出(400)特定服务器的错误请求

错误 400 错误请求 AJAX (WCF)

Post 请求WCF接口报:远程服务器返回错误: (415)

Restful WCF 服务 POST 方法在 fiddler2 中返回 HTTP400 错误

传递流时出现 Wcf Rest Service 错误