123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- public class URLRequestData
- {
- private bool encrypt;
- private List<KeyValuePair<string, object>> data;
- private KeyValuePair<string, byte[]> file;
- public URLRequestData(bool encrypt=false)
- {
- this.encrypt = encrypt;
- data = new List<KeyValuePair<string, object>>();
- file = new KeyValuePair<string, byte[]> ();
- }
- public void Add(string key, object value)
- {
- data.Add(new KeyValuePair<string, object>(key, value));
- }
- public void AddFile(string key, byte[] file)
- {
- this.file = new KeyValuePair<string, byte[]> (key, file);
- }
- public bool HasData()
- {
- return data.Count > 0 || !StringUtil.Empty(file.Key);
- }
- public string GetDataString()
- {
- string info = "";
- string result = "";
- if(encrypt)
- {
- JsonData json = JsonMapper.ToObject("{}");
- for(int i=0; i<data.Count; i++)
- {
- KeyValuePair<string, object> kvp = data[i];
- info += "["+kvp.Key+" : "+kvp.Value+"] ";
- json[kvp.Key] = kvp.Value.ToString();
- }
- result = "v="+DecryptionUtil.Encryption(json.ToJson());
- }
- else
- {
- string[] strArr = new string[data.Count];
- for(int i=0; i<data.Count; i++)
- {
- KeyValuePair<string, object> kvp = data[i];
- info += "["+kvp.Key+" : "+kvp.Value+"] ";
- strArr[i] = kvp.Key+"="+kvp.Value;
- }
- result = string.Join("&", strArr);
- }
- Debuger.Log("Url data : "+info);
- return result;
- }
- public WWWForm GetDataForm()
- {
- string info = "";
- WWWForm form = new WWWForm();
- if(encrypt)
- {
- JsonData json = JsonMapper.ToObject("{}");
- for(int i=0; i<data.Count; i++)
- {
- KeyValuePair<string, object> kvp = data[i];
- info += "["+kvp.Key+" : "+kvp.Value+"] ";
- json[kvp.Key] = kvp.Value.ToString();
- }
- form.AddField("v", DecryptionUtil.Encryption(json.ToJson()));
- }
- else
- {
- for(int i=0; i<data.Count; i++)
- {
- KeyValuePair<string, object> kvp = data[i];
- info += "["+kvp.Key+" : "+kvp.Value+"] ";
- form.AddField(kvp.Key, kvp.Value.ToString());
- }
- }
- if(!StringUtil.Empty(file.Key))
- {
- form.AddBinaryData (file.Key, file.Value);
- info += "[" + file.Key + " : bytes " + file.Value.Length + "]";
- }
- Debuger.Log("Url data : "+info);
- return form;
- }
- }
|