.net WCF Rest service post方法未找到终结点
我无法使用post方法调用rest服务,并不断获取端点未找到错误。代码如下:.net WCF Rest service post方法未找到终结点,.net,wcf,rest,iis,wcf-binding,.net,Wcf,Rest,Iis,Wcf Binding,我无法使用post方法调用rest服务,并不断获取端点未找到错误。代码如下: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke( Method = "POST", UriTemplate = "GetData", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMe
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "GetData",
BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json
)]
string GetData(string value);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1 : IService1
{
public string GetData(string value)
{
return value;
}
}
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxUrlLength="1048576" relaxedUrlToFileSystemMapping="true" />
</system.web>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding" />
</protocolMapping>
<services>
<service name="RestPost.Service1" behaviorConfiguration="default">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="RestPost.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web" >
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="default">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<!-- default binding configration used for all REST services -->
<webHttpBinding>
<!-- max allowed message size incresed to 500 000 Bytes -->
<binding maxBufferSize="95000000" maxReceivedMessageSize="95000000" />
</webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<security>
<requestFiltering>
<requestLimits maxUrl="40960000" maxQueryString="20480000" maxAllowedContentLength="20480000" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
这就是我用小提琴来称呼它的方式
我现在正试着在Casini的领导下处理这件事。最终它将部署在IIS7.5上
如果您想知道为什么要传递一个大的base64编码字符串,那么我之所以这样做是因为我需要以JSON格式发布一个请求。现在,由于JSON具有IIS立即拒绝的特殊字符,我尝试使用URLencode。问题是你不能超过1000个字符左右。长度是有限的。Base64编码和使用post是唯一的方法,这就是为什么我要使用这段代码
此Rest服务的最初目标是能够为基于javascript的客户机提供服务,该客户机将向此服务发送JSON帖子,并获得JSON响应。没有xml字符串填充的纯JSON响应
需要帮助才能使post到rest服务正常工作。通过浏览器调用它时失败的原因:浏览器发出的是
GET
请求,而不是post
请求
通过Fiddler调用时失败的原因:您的内容类型是“application/x-www-form-urlencoded”,但内容不是(它是base64 blob)。即使您有一个格式很好的表单urlencoded data(例如
a=foo&b=bar
),这仍然不起作用,因为WCF不支持这种开箱即用的格式(您可以使用一些扩展点来添加支持,但它需要更多的工作)
需要执行的操作:您的操作需要一个字符串
参数。在Fiddler中,您可以将数据的base64编码作为JSON字符串传递(即,将其包装在“
)中)。此外,还可以设置正确的内容类型:
POST http://localhost:57395/Service1.svc/getdata
Content-Type: application/json
Host: localhost:57395
Content-Length: <the appropriate length; fiddler will set it for you>
"large base64 string here"
POSThttp://localhost:57395/Service1.svc/getdata
内容类型:application/json
主机:本地主机:57395
内容长度:
“此处为大base64字符串”
实际上,为了发送base64输入,我只需要将输入参数从字符串更改为流,当我从Fiddler进行测试时,它工作正常。我将内容类型保留为“application/x-www-form-urlencoded”“。关于浏览器生成get而不是post,您是对的。对的,您还可以使用接收任意数据,例如在您的案例中编码的表单。
POST http://localhost:57395/Service1.svc/getdata
Content-Type: application/json
Host: localhost:57395
Content-Length: <the appropriate length; fiddler will set it for you>
"large base64 string here"