C# wcf Rest服务中的POST方法
可能重复:C# wcf Rest服务中的POST方法,c#,wcf,C#,Wcf,可能重复: 我无法在wcf Rest服务中使用POST方法。请帮助获取以下代码 接口实现 [OperationContract] [WebInvoke( UriTemplate = "/SendMail",Method ="POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)] EmailDetails S
我无法在wcf Rest服务中使用POST方法。请帮助获取以下代码 接口实现
[OperationContract]
[WebInvoke( UriTemplate = "/SendMail",Method ="POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
EmailDetails SendMail(EmailDetails rData);
protected void Button1_Click(object sender, EventArgs e)
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.ReaderQuotas.MaxStringContentLength = 2000000;
binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.CloseTimeout = new TimeSpan(4, 0, 0);
binding.OpenTimeout=new TimeSpan(4, 0, 0);
binding.ReceiveTimeout=new TimeSpan(2, 0, 0);
binding.SendTimeout = new TimeSpan(5, 0, 0);
EndpointAddress endpoint = new EndpointAddress(new Uri("http://localhost:35798/RestServiceImpl.svc"));
RestPostService.RestServiceImplClient obj = new RestPostService.RestServiceImplClient(binding, endpoint);
RestPostService.EmailDetails obj1 = new RestPostService.EmailDetails();
obj.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
RestPostService.EmailDetails obj2=obj.SendMail(obj1);
}
它返回一个错误:找不到远程服务器,有时
服务器没有提供有意义的回复;这可能是由于契约不匹配、会话过早关闭或内部服务器错误造成的。将其放在web.config文件中,它将帮助您找出web服务无法工作的原因:
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "c:\temp\WEBTraces.log" />
</listeners>
</source>
</sources>
</system.diagnostics>
将其放入web.config文件,它将帮助您找出web服务无法工作的原因:
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "c:\temp\WEBTraces.log" />
</listeners>
</source>
</sources>
</system.diagnostics>
如果您是Web服务的所有者和使用者,请尝试删除WebInvokeAttribute。或者至少删除该模板。仅查看按钮单击中提供的代码,它不是必需的,可能会导致您的问题。如果您是Web服务的所有者和使用者,请尝试删除WebInvokeAttribute。或者至少删除该模板。仅查看按钮单击中提供的代码,它不是必需的,可能会导致您的问题。REST使用简单的Http协议,使用Get/Post/Put/Delete动词
BasicHttpBinding用于基于SOAP的web服务
为了使用Http协议的POST谓词向WCF服务发出请求,您需要使用类或某些第三方库,如RestSharp
当使用REST样式时,客户机和服务器之间没有任何代理手段来执行请求,就像在基于SOAP的web服务中一样
请查看下面的链接以构建一些REST WCF服务以及如何使用它们
REST在简单的Http协议上使用Get/Post/Put/Delete动词
BasicHttpBinding用于基于SOAP的web服务
为了使用Http协议的POST谓词向WCF服务发出请求,您需要使用类或某些第三方库,如RestSharp
当使用REST样式时,客户机和服务器之间没有任何代理手段来执行请求,就像在基于SOAP的web服务中一样
请查看下面的链接以构建一些REST WCF服务以及如何使用它们