传递JSON字符串列表格式不正确的字符串?

时间:2019-12-07 09:42:54

标签: c#

我正在将字符串列表保存到后端api,但是以某种方式使字符串格式错误。我已经检查了所有可能的事情,但是我48个小时都没有入睡,所以我很想念它。

似乎用amp代替_-不确定。

更正字符串(也是持久化之前的字符串)

&_nc_cat=100&oh=2709db2b099e5eb5bb6eb19654de828a&oe=5E7FF7C5

持久后的字符串

&_nc_cat=100&oh=2cda55a0255b0ef3afabe535412a43f8&oe=5E7FF7C5

完整的原始正确字符串实际上是 https://scontent-lhr3-1.cdninstagram.com/s640x640/75448871_111514890143520_4913259559873269659_n.jpg?_nc_ht=scontent-lhr3-1.cdninstagram.com&_nc_cat=100&oh=2709db2b099e5eb5bb6eb19654de828a&oe=5E7FF7C5

这是我保存到api的方式

var response = HttpFactory.PushItemToCache(new Dictionary<string, object>
{
    { "item", Url },
    { "username", Username },
    { "name", Name },
    { "picture", Picture },
    { "pictures", Pictures },
});

Http方法:

public static IRestResponse PushItemToCache(Dictionary<string, object> data)    
{
    return HttpUtilities.Post("/api/cache", data);
}

发布方法:

public static IRestResponse PerformResource(string resource, IDictionary<string, object> data = default, string method = "GET", bool checkToken = true)
{
    var sw = Stopwatch.StartNew();

    var request = new RestRequest(resource);

    request.AddHeader("Accept", "application/json");
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("Authorization", $"Bearer {_bearerToken}");

    if (data == null)
    {
        data = new Dictionary<string, object>();
    }

    data.Add("owner", Environment.UserName + Environment.MachineName);

    if (method != "GET")
    {
        request.AddJsonBody(data);
    }
    else
    {
        foreach (var pair in data)
        {
            request.AddParameter(pair.Key, pair.Value);
        }
    }

    IRestResponse response;

    switch (method)
    {
        case "POST":
            response = RestClient.Post(request);
            break;
        case "DELETE":
            response = RestClient.Delete(request);
            break;
        case "PUT":
            response = RestClient.Delete(request);
            break;
        case "PATCH":
            response = RestClient.Patch(request);
            break;
        default:
            response = RestClient.Get(request);
            break;
    }

    if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.NotFound)
    {
        new ConsoleLogger().Error("Response for " + resource + " returned " + response.StatusDescription);
        new ConsoleLogger().Error(response.Content);
        new ConsoleLogger().Error(response.ErrorMessage);
    }

    if (sw.Elapsed.TotalSeconds >= 5)
    {
        new ConsoleLogger().Pink($"This request ({resource}) took {sw.Elapsed.TotalSeconds} seconds");
    }

    return response;
}

1 个答案:

答案 0 :(得分:0)

好像您的字符串是url编码的,您可以使用HttpUtility.UrlDecode

将其转换回来

working example here