防止编码RestSharp参数

时间:2017-04-07 09:07:10

标签: c# restsharp

我尝试使用范围参数集t o OAuth2调用read,usercp API。但是,RestSharp始终将参数编码为&scope=read%2Cusercp,而应该是&scope=read,usercp

我还没有找到一种方法来禁用单个参数的编码。

这是我的代码:

var request = GetRequest("index.php?oauth/token", Method.POST);
request.AddParameter("client_id", ClientId);
request.AddParameter("client_secret", ClientSecret);

request.AddParameter("grant_type", "password");
request.AddParameter("username", username);
request.AddParameter("password", password);
request.AddParameter("scope", "read,usercp");

//request.Parameters.Add(new Parameter
//{
//    ContentType = "application/json",
//    Name = "scope",
//    Value = "read,usercp"
//});

var response = await RestClient.ExecuteTaskAsync<AuthenticateResponse>(request);
if (response.StatusCode != HttpStatusCode.OK &&
    response.StatusCode != HttpStatusCode.Accepted)
{
    throw new Exception("Could not authenticate user");
}

如何禁用该单个参数的编码?

1 个答案:

答案 0 :(得分:-1)

由于我假设您希望逗号被完全按照代码中的描述进行处理,我建议使用逐字字符串转义。 不完全确定RestSharp如何处理这个,但你可以尝试:

    request.AddParameter("scope", @"read,usercp");

或者你单独用字符串转义它:

    var sScope = @"read,usercp";
    request.AddParameter("scope", sScope);