假设我有一个接受电子邮件id和密码作为url参数的web服务。我必须通过电子邮件id和密码验证用户。using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;using System.ServiceModel.Web;namespace WebApp.Services{[ServiceContract]public interface IService { [WebInvoke(Method = "GET", UriTemplate = "/authenticate/{emailId}/{password}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] [OperationContract] Boolean Authenticate(string emailId, string password); }}
我们这样调用web服务:
http://localhost: 14176/服务/Service.svc/验证/sushant.bhatnagar@greatdevelopers.com/123
因为电子邮件包含'。
,该参数没有被浏览器编码,因此没有调用web服务函数。在url中传递email id除了查询字符串之外还有其他解决方案
如果你可以使用POST:
[ServiceContract]
public interface IMyService
{
[OperationContract]
bool Authenticate(EmailCredential request);
}
[DataContract]
public class EmailCredential
{
[DataMember]
public string EmailId {get; set;}
[DataMember]
public string Password {get; set;}
}
和调用服务使用WebClient或WebHttpRequest与xml(我不知道现在json看起来像这个xml)
<EmailCredential >
<EmailId >sushant.bhatnagar@greatdevelopers.com</EmailId >
<Password >123</Password >
</EmailCredential >