WCF中的特殊字符

问题描述 投票:0回答:1

我使用SHA-1加密密码并将其发送到服务,如下所示。

以下密码正在加密。

客户端

HttpGetCommand.cs

public class HttpGetCommand<TResult> : AbstractHttpCommand<TResult>
    where TResult : class
{
    private readonly Dictionary<string, object> _parameters = new Dictionary<string, object>();

    protected override string TypeRequest { get { return "GET"; } }

    public HttpGetCommand(string url)
        : base(url)
    { }

    public HttpGetCommand(string url, Action<TResult> successAction, Action<Exception> errorAction)
        : base(url, successAction, errorAction)
    {
    }

    public HttpGetCommand<TResult> AddParameter(object urlParameter)
    {
        AddUrlParameter(urlParameter);
        return this;
    }

    public HttpGetCommand<TResult> AddParameter(string name, object value)
    {
        _parameters.Add(name, value);
        return this;
    }

    public T AddParameter<T>(string name, object value)
        where T : HttpGetCommand<TResult>
    {
        _parameters.Add(name, value);
        return (T)this;
    }

    public override TResult Execute()
    {
        return _httpService.Get<TResult>(Url, _parameters);
    }

    public async override Task ExecuteAsync()
    {
        await _httpService.GetAsync(Url, _parameters, SuccessAction, ErrorAction);
    }

    public override string ToString()
    {
        return base.ToString() + GetParams();
    }

    private string GetParams()
    {
        if (!_parameters.Any())
        {
            return string.Empty;
        }
        var sb = new StringBuilder();
        sb.Append("?");
        foreach (var parameter in _parameters)
        {
            sb.AppendFormat("{0}&{1}", parameter.Key, parameter.Value.ToString().Replace(' ', '+'));
        }
        return sb.ToString();
    }
}

LoginCommand.cs

public class LoginCommand : HttpGetCommand<LoginResult>
{
    public LoginCommand()
        : base("Login")
    {
    }
    public LoginCommand(Action<LoginResult> successAction, Action<Exception> errorAction)
        : base("Login", successAction, errorAction)
    {
    }

    public LoginCommand SetUsername(string username)
    {
        return AddParameter<LoginCommand>("username", username);
    }

    public LoginCommand SetPassword(string password)
    {
        return AddParameter<LoginCommand>("userpassword", password);
    }
}

在代码中,我调用如下

 return new LoginCommand()
            .SetUsername(username)
            .SetPassword(password)
            .Execute();

服务方面

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Stream Login(string username, string userpassword);

但是如果encyrtion具有诸如“+”之类的特殊字符,则它不会与服务联系。

例如,如果来自客户端的加密密码就好

d908980/fhjdfgf89sdsd+sdsd

然后按如下方式到达服务方

d908980/fhjdfgf89sdsd sdsd
c# wcf webinvoke
1个回答
0
投票

您对GetParams的实施存在缺陷。您需要确保以正确的查询字符串格式正确编码查询字符串。

sb.AppendFormat("{0}={1}&", 
    Uri.EscapeDataString(parameter.Key), 
    Uri.EscapeDataString(parameter.Value.ToString())
);
© www.soinside.com 2019 - 2024. All rights reserved.