3

I have created a list of KeyValuePair to populate the content as the data for the HttpClient.

List<KeyValuePair<string, string>> keyValues = new List<KeyValuePair<string, string>>();

keyValues.Add(new KeyValuePair<string, string>("email", email));
keyValues.Add(new KeyValuePair<string, string>("password", password));
keyValues.Add(new KeyValuePair<string, string>("plan_id", planId));

var content = new FormUrlEncodedContent(keyValues);

But later I found I have to send a int value as the plan_id. How can change the above list to accept KeyValuePair. Or is there any better way to do this?

Yasitha
  • 2,233
  • 4
  • 24
  • 36
  • 4
    Ultimately everything in HTTP is sent as an ASCII string, so why not just call `planId.ToString(CultureInfo.InvariantCulture)`? – Dai Apr 21 '15 at 02:52
  • I am calling an external API. When I send a string value for the plan id I am getting a Bad Request. But with a int value it is getting success. – Yasitha Apr 21 '15 at 02:56
  • Is it possible to create generic list as follow in java. List> keyValues = new List>(); – Yasitha Apr 21 '15 at 02:59
  • 1
    Well `FormUrlEncodedContent` will only accept an `IEnumerable> nameValueCollection` so unless you encode your content differently that's what you will have to do and as @Dai mentioned, convert your integer to its string representation. – Alex Apr 21 '15 at 03:03

3 Answers3

4

If you want to create a KeyValuePair list, you should create Dictionary.

Dictionary<string, string> dic = new Dictionary<string,string>();
dic.Add("email", email);
dic.Add("password", password);
dic.Add("plan_id", planId.ToString());
Black Frog
  • 11,595
  • 1
  • 35
  • 66
4

Use KeyValuePair<string, object> to put values and create or convert the list to KeyValuePair<string, string> when to use FormUrlEncodedContent

Creating a new list

List<KeyValuePair<string, object>> keyValues = new List<KeyValuePair<string, object>>();

keyValues.Add(new KeyValuePair<string, object>("email", "asdasd"));
keyValues.Add(new KeyValuePair<string, object>("password", "1131"));
keyValues.Add(new KeyValuePair<string, object>("plan_id", 123));
keyValues.Add(new KeyValuePair<string, object>("other_field", null));

var content = new FormUrlEncodedContent(keyValues.Select(s => 
    new KeyValuePair<string, string>(s.Key, s.Value != null ? s.ToString() : null)
));

Convert list

public static KeyValuePair<string, string> ConvertRules(KeyValuePair<string, object> kv)
{
    return new KeyValuePair<string, string>(kv.Key, kv.Value != null ? kv.ToString() : null);
}

static Task Main(string[] args) 
{
    List<KeyValuePair<string, object>> keyValues = new List<KeyValuePair<string, object>>();

    keyValues.Add(new KeyValuePair<string, object>("email", "asdasd"));
    keyValues.Add(new KeyValuePair<string, object>("password", "1131"));
    keyValues.Add(new KeyValuePair<string, object>("plan_id", 123));
    keyValues.Add(new KeyValuePair<string, object>("other_field", null));

    var content = new FormUrlEncodedContent(keyValues.ConvertAll(ConvertRules)));
));
Jeferson Tenorio
  • 2,030
  • 25
  • 31
0

Don't use List<KeyValuePair<string,string>>, instead of Dictionary<string, string>. use planId.ToString().

zhangyiying
  • 414
  • 4
  • 15