对NameValueCollection C#.net中的值进行编码
我有一个名值集合,它被传递到一个方法中,通过web客户端发送到另一个系统对NameValueCollection C#.net中的值进行编码,c#,.net,namevaluecollection,C#,.net,Namevaluecollection,我有一个名值集合,它被传递到一个方法中,通过web客户端发送到另一个系统 public string DoExtendedTransferAsString(string operation, NameValueCollection query, FormatCollection formats) { System.Net.WebClient client = new System.Net.WebClient(); client.QueryString = query; c
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
感谢来自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循环中为集合中的每个值执行编码部分
如果这不是您想要的,请在问题中更加明确。来自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循环中为集合中的每个值执行编码部分
如果这不是您想要的,请在问题中更加明确。我认为这将实现您想要的
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并将其放回原位。我认为这将实现您想要的
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,然后将其放回原位。试试这个
myCollection.AllKeys
.ToList()
.ForEach(k => myCollection[k] =
HttpUtility.HtmlEncode(myCollection[k]));
试试这个
myCollection.AllKeys
.ToList()
.ForEach(k => myCollection[k] =
HttpUtility.HtmlEncode(myCollection[k]));
@Liviu我很抱歉,我认为我是具体的,我想对NameValueCollection中的值执行此操作。我尝试了(int I=0;I