Skip to main content
 首页 » 编程设计

c#之如何将参数传递给带有特殊字符的 WCF Web 服务

2024年05月06日51zhenyulu

我将 WCF 与 C# 结合使用。

我必须将参数传递给带有特殊字符的 WCF Web 服务,例如- Email-ram@gmail.com 和 Password-Test@123

传递参数时,出现错误“404 Not Found”,如果传递的参数没有特殊字符,它可以正常工作。

工作

localhost:35798/RestServiceImpl.svc/LoginRequest/ram/Test123/1223 

不工作

localhost:35798/RestServiceImpl.svc/LoginRequest/ram@gmail.com/Test@123/1223 

这可能吗?

/** Code for the same **/ 
[OperationContract] 
    [WebInvoke(  
    Method = "POST",  
    RequestFormat = WebMessageFormat.Json,  
    ResponseFormat = WebMessageFormat.Json,  
    BodyStyle = WebMessageBodyStyle.WrappedRequest,  
    UriTemplate = "LoginRequest/{Email}/{Password}/{APPUID}")] 
 
string LoginRequest(string Email, string Password, string APPUID); 
 
public string LoginRequest(string Email, string Password, string APPUID) 
{ 
    string strResult = ""; 
    if (Password.Trim() != null && Password.Trim() != "" && Email.Trim() != null && Email.Trim() != "") 
        { 
            strResult = " Success."; 
        } 
    else 
        { 
            strResult = "User name and password are required."; 
        } 
    return strResult; 
} 

请您参考如下方法:

您应该通过Html encoding 发送参数部分。 :

var safeEmailParam = WebUtility.HtmlEncode(yourEMailParam);