对 NameValueCollection C# .net 中的值进行编码

发布于 2024-10-05 15:50:53 字数 643 浏览 2 评论 0原文

我有一个名称值集合,它被传递到一个方法中,通过 Web 客户端发送到另一个系统。

public string DoExtendedTransferAsString(string operation, NameValueCollection query, FormatCollection formats)
{
    System.Net.WebClient client = new System.Net.WebClient();
    client.QueryString = query;
    client.QueryString["op"] = operation;
    client.QueryString["session"] = SessionId;
    using (Stream stream = client.OpenRead(url))
    {
        FormatCollection formats = new FormatCollection(stream);
    }
    return formats;
}

我需要对 NameValueCollection 内的所有值运行 HttpUtility.HtmlEncode,但我不确定如何操作。注意我无法更改调用代码,因此它必须是 NameValueCollection。

谢谢

I have a name value collections which is passed into a method to send to another system via a web client.

public string DoExtendedTransferAsString(string operation, NameValueCollection query, FormatCollection formats)
{
    System.Net.WebClient client = new System.Net.WebClient();
    client.QueryString = query;
    client.QueryString["op"] = operation;
    client.QueryString["session"] = SessionId;
    using (Stream stream = client.OpenRead(url))
    {
        FormatCollection formats = new FormatCollection(stream);
    }
    return formats;
}

I need to run HttpUtility.HtmlEncode on all the values inside the NameValueCollection but I am unsure how to. NB I cannot change the calling code so it have to be a NameValueCollection.

Thanks

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

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

发布评论

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

评论(3

泪冰清 2024-10-12 15:50:53

试试这个

myCollection.AllKeys
    .ToList()
    .ForEach(k => myCollection[k] = 
            HttpUtility.HtmlEncode(myCollection[k]));

try this

myCollection.AllKeys
    .ToList()
    .ForEach(k => myCollection[k] = 
            HttpUtility.HtmlEncode(myCollection[k]));
梦回梦里 2024-10-12 15:50:53

来自 MSDN:

class MyNewClass
   {
      public static void Main()
      {
         String myString;
         Console.WriteLine("Enter a string having '&' or '\"'  in it: ");
         myString=Console.ReadLine();
         String myEncodedString;
         // Encode the string.
         myEncodedString = HttpUtility.HtmlEncode(myString);
         Console.WriteLine("HTML Encoded string is "+myEncodedString);
         StringWriter myWriter = new StringWriter();
         // Decode the encoded string.
         HttpUtility.HtmlDecode(myEncodedString, myWriter);
         Console.Write("Decoded string of the above encoded string is "+
                        myWriter.ToString());
      }
   }

您可以在 for/foreach 循环中对集合中的每个值进行编码部分。

如果这不是您想要的,请在问题中更加明确。

From MSDN:

class MyNewClass
   {
      public static void Main()
      {
         String myString;
         Console.WriteLine("Enter a string having '&' or '\"'  in it: ");
         myString=Console.ReadLine();
         String myEncodedString;
         // Encode the string.
         myEncodedString = HttpUtility.HtmlEncode(myString);
         Console.WriteLine("HTML Encoded string is "+myEncodedString);
         StringWriter myWriter = new StringWriter();
         // Decode the encoded string.
         HttpUtility.HtmlDecode(myEncodedString, myWriter);
         Console.Write("Decoded string of the above encoded string is "+
                        myWriter.ToString());
      }
   }

You do the encoding part for each value in the collection in a for/foreach loop.

If this is not what you were looking for please be more explicit in the question.

岁吢 2024-10-12 15:50:53

我认为这将实现您想要的...

    public string DoExtendedTransferAsString(string operation, NameValueCollection query, FormatCollection formats)
    {
        foreach (string key in query.Keys)
        {
            query[key] = HttpUtility.HtmlEncode(query[key]);
        }

        System.Net.WebClient client = new System.Net.WebClient();
        client.QueryString = query;
        client.QueryString["op"] = operation;
        client.QueryString["session"] = SessionId;
        using (Stream stream = client.OpenRead(url))
        {
            FormatCollection formats = new FormatCollection(stream);
        }
        return formats;
    }

请注意我在其中添加的 foreach,您只是迭代所有键,使用键抓取每个项目并对其调用 HtmlEncode 并将其放回原处。

I think this will accomplish what you want...

    public string DoExtendedTransferAsString(string operation, NameValueCollection query, FormatCollection formats)
    {
        foreach (string key in query.Keys)
        {
            query[key] = HttpUtility.HtmlEncode(query[key]);
        }

        System.Net.WebClient client = new System.Net.WebClient();
        client.QueryString = query;
        client.QueryString["op"] = operation;
        client.QueryString["session"] = SessionId;
        using (Stream stream = client.OpenRead(url))
        {
            FormatCollection formats = new FormatCollection(stream);
        }
        return formats;
    }

Note the foreach I added in there, you're just iterating through all the keys, grabbing each item using the key and calling HtmlEncode on it and putting it right back.

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