I've already read this. But there's no example to make it work. So I tried it on my own. Here's my code:
public void AskServer(List<Kvp> kvps)
{
WWWForm form = new WWWForm();
Hashtable headers = form.headers;
if (this._lastCookies != string.Empty) {
headers.Add("Cookie", this._lastCookies);
}
foreach (var arg in kvps) {
form.AddField(arg.Key, arg.Value.ToString());
}
form.AddField("pseudo", this._pseudo);
form.AddField("jeton", this._dernierJeton.ToString());
StartCoroutine(SendToServer(
new WWW(this._URL, form.data, headers)
));
}
Now, there's a warning saying that calling new WWW(this._URL, form.data, headers)
is obsolete, I should use the one with the dictionnary. The declarations are like this:
public WWW(string url, byte[] postData, Dictionary<string, string> headers);
[Obsolete("This overload is deprecated. Use the one with Dictionary argument.")]
public WWW(string url, byte[] postData, Hashtable headers);
So when I try to use the example in the link I've provided at the beginning of the question, I have a code like this, which doesn't work:
public static Dictionary<K, V> HashtableToDictionary<K, V>(Hashtable table)
{
return table
.Cast<DictionaryEntry>()
.ToDictionary(kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}
public void AskServer(List<Kvp> kvps)
{
WWWForm form = new WWWForm();
Dictionary<string, string> headers = StateManager.HashtableToDictionary<string, object>(form.headers);
if (this._lastCookies != string.Empty) {
headers.Add("Cookie", this._lastCookies);
}
foreach (var arg in kvps) {
form.AddField(arg.Key, arg.Value.ToString());
}
form.AddField("pseudo", this._pseudo);
form.AddField("jeton", this._dernierJeton.ToString());
StartCoroutine(SendToServer(
new WWW(this._URL, form.data, headers)
));
}
The error is: Assets/Code/StateManager.cs(58,36): error CS0029: Cannot implicitly convert type
System.Collections.Generic.Dictionary' to System.Collections.Generic.Dictionary<string,string>'
What am I doing wrong? And is there a more effective way of doing this?