![](/img/trans.png)
[英]Why When I Type “POST” Method for Wcf Service It Gives “Endpoint not found” Error?
[英]WCF Rest service post method gives endpoint not found
我無法使用post方法調用rest服務,並不斷收到未找到端點的錯誤。 下面的代碼:
[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>
這就是我在瀏覽器中調用網址的方式
http://localhost:57395/Service1.svc/getdata/large base64encoded string here
這就是我在提琴手中的稱呼
我現在正嘗試在Casini下運行此程序。 最終它將部署在IIS 7.5上。
如果您想知道為什么要傳遞一個大的base64編碼的字符串,我這樣做是因為我需要以JSON格式發布請求。 現在,由於JSON具有IIS立即拒絕的特殊特征,因此我嘗試使用URLencode。 問題是您不能超過1000個字符。 長度是有限的。 Base64編碼和使用post是唯一的方法,這就是為什么我要使用此代碼。
Rest服務的最初目標是能夠為基於JavaScript的客戶端提供服務,該客戶端將對該服務進行JSON發布並獲得JSON響應作為回報。 不含xml字符串填充的純JSON響應。
需要幫助以將帖子發布到其余服務上班。
為什么通過瀏覽器調用時失敗:瀏覽器正在發出GET
請求,而不是POST
請求。
為什么通過Fiddler調用它時失敗:您的內容類型為“ application / x-www-form-urlencoded”,但內容不是(它是base64 blob)。 即使您擁有格式正確的表單編碼數據(例如a=foo&b=bar
),也仍然無法使用,因為WCF不支持即用型格式(您可以使用其中的一些可擴展性以添加支持,但需要更多工作)。
您需要做的是:您的操作需要一個string
參數。 在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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.