对 NameValueCollection C# .net 中的值进行编码
我有一个名称值集合,它被传递到一个方法中,通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
try this
来自 MSDN:
您可以在 for/foreach 循环中对集合中的每个值进行编码部分。
如果这不是您想要的,请在问题中更加明确。
From MSDN:
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.
我认为这将实现您想要的...
请注意我在其中添加的 foreach,您只是迭代所有键,使用键抓取每个项目并对其调用 HtmlEncode 并将其放回原处。
I think this will accomplish what you want...
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.