POST方法在WCF REST服务
问题描述:
Possible Duplicate:
POST Method in wcf Rest ServicePOST方法在WCF REST服务
我不能使用POST方法在WCF休息service.Please帮助,下面的代码
接口实现
[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>
更多的这样的位置:http://msdn.microsoft.com/en-us/library/ms733025.aspx
答
如果你是主人和Web服务的消费者,请尝试删除WebInvokeAttribute。或者至少删除UriTemplate。只需单击按钮中提供的代码,就不需要它,并可能导致您的问题。
答
REST使用Get/Post/Put/Delete动词来处理简单的Http协议。
BasicHttpBinding用于基于SOAP的Web服务。
为了使用Http协议的POST动词向WCF服务发出请求,您需要使用WebRequest类或第三方库(如RestSharp)。
当使用REST风格时,客户端和服务器之间没有任何代理方式来执行请求,因为它在基于SOAP的Web服务中。
看一看下面的链接建立一些REST WCF服务,以及如何使用它们