在 C# 中,如何构造和解析嵌套查询字符串?

发布于 2024-12-29 05:54:20 字数 447 浏览 0 评论 0原文

我正在编写一个 API,希望人们能够提供 Google Charts API 调用作为参数。在参数包含完全独立的 API 调用的情况下,解构这个有问题的 API 调用的正确方法是什么?

例如:

?method=createimage&chart1=https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World

在上面的示例中,我想将其视为 (2) 个查询字符串键:methodchart1。我是否可以将上面的示例解析为 2 个查询字符串键,使 Google Charts API 调用保持完整,而不是将其分解?我可以将调用封装为 JSON 或类似的内容吗?

非常感谢!干杯

I'm writing an API and would like people to be able to supply a Google Charts API call as an argument. What's the proper way to deconstruct this problematic API call, where an argument contains an entirely separate API call?

For instance:

?method=createimage&chart1=https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World

In the example above, I'd like to think of it as (2) query string keys: method and chart1. Is it possible for me to parse the above example as 2 query string keys, leaving the Google Charts API call intact, rather than breaking it down? Could I enclose the call as JSON or something along those lines?

Thanks much! Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2025-01-05 05:54:20

这是正确的方法(使用 ParseQueryString 方法):

using System;
using System.Web;

class Program
{
    static void Main()
    {
        var query = "?method=createimage&chart1=https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World";
        var values = HttpUtility.ParseQueryString(query);
        Console.WriteLine(values["method"]);
        Console.WriteLine(values["chart1"]);
    }
}

如果您愿意构造此查询字符串:

using System;
using System.Web;

class Program
{
    static void Main()
    {
        var values = HttpUtility.ParseQueryString(string.Empty);
        values["method"] = "createimage";
        values["chart1"] = "https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World";
        Console.WriteLine(values);
        // prints "method=createimage&chart1=https%3a%2f%2fchart.googleapis.com%2fchart%3fchs%3d250x100%26chd%3dt%3a60%2c40%26cht%3dp3%26chl%3dHello%7cWorld"
    }
}

哦,顺便说一下,您在问题中显示的是一个无效的查询字符串,我所显示的第二个代码片段的输出证实了这一点。您应该对 chart1 参数进行 URL 编码。查询字符串中包含多个 ? 字符绝对违反所有标准。

正确的查询字符串如下所示:

?method=createimage&chart1=https%3A%2F%2Fchart.googleapis.com%2Fchart%3Fchs%3D250x100%26chd%3Dt%3A60%2C40%26cht%3Dp3%26chl%3DHello%7CWorld

Here's the proper way (using the ParseQueryString method):

using System;
using System.Web;

class Program
{
    static void Main()
    {
        var query = "?method=createimage&chart1=https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World";
        var values = HttpUtility.ParseQueryString(query);
        Console.WriteLine(values["method"]);
        Console.WriteLine(values["chart1"]);
    }
}

and if you wanted to construct this query string:

using System;
using System.Web;

class Program
{
    static void Main()
    {
        var values = HttpUtility.ParseQueryString(string.Empty);
        values["method"] = "createimage";
        values["chart1"] = "https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World";
        Console.WriteLine(values);
        // prints "method=createimage&chart1=https%3a%2f%2fchart.googleapis.com%2fchart%3fchs%3d250x100%26chd%3dt%3a60%2c40%26cht%3dp3%26chl%3dHello%7cWorld"
    }
}

Oh and by the way, what you have shown in your question is an invalid query string which is confirmed by the output of the second code snippet I have shown. You should URL encode your chart1 parameter. It's absolutely against all standards to have more than one ? character in a query string.

Here's how the correct query string would look like:

?method=createimage&chart1=https%3A%2F%2Fchart.googleapis.com%2Fchart%3Fchs%3D250x100%26chd%3Dt%3A60%2C40%26cht%3Dp3%26chl%3DHello%7CWorld
_失温 2025-01-05 05:54:20

您应该对查询字符串中的 URL 进行 URL 编码,因为它包含保留字符。另外,十六进制编码也可以正常工作。

完成此操作后,您可以分别处理这两个值,并且解析很简单。

You should URL encode the URL in your query string, as it contains reserved characters. Alternatively, hex encoding works just fine too.

Once you have done so, you can treat the two values separately and parsing is simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文