After I tried to initialize a NameValueCollection with an inner NameValueCollection, the c# compiler says that has to be NameValueCollection(string,string).
As I thought when I call the Add function when I initialize like this.
Is there any other approach that I can initialize the same on one line, based on NameValueCollection?
The reason is that I have to POST data to an HTTPAddress and I use this function
Here is my example which won't worked:
NameValueCollection data = new NameValueCollection()
{
{ "key1", "value1"},
{ "key2", new NameValueCollection()
{
{ "key2sub", "value"}
}
};
Not really.The UploadValues(..)
method take a NameValueCollection
, and a NameValueCollection
only accepts string
.
The aim of the method is to transform your data...
UploadValues("http://www.example.com/", new NameValueCollection()
{
{ "key1", "value1" },
{ "key2", "value2" }
};
... into the URL:
http://www.example.com/?key1=value1&key2=value2
If you want to send more information, you can put a delimited string as the value for one of your keys, and then split it apart again at the other end.
UploadValues("http://www.example.com/", new NameValueCollection()
{
{ "key1", "value1" },
{ "key2", "value2,value3,value4" }
};
Which should produce something like:
http://www.example.com/?key1=value1&key2=value2,value3,value4
But the best solution is as @hmnzr said in their answer (now-deleted), to avoid NameValueCollection
entirely and upload a custom object serialized as JSON using the UploadString(..)
method.