URLRequestData.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. public class URLRequestData
  6. {
  7. private bool encrypt;
  8. private List<KeyValuePair<string, object>> data;
  9. private KeyValuePair<string, byte[]> file;
  10. public URLRequestData(bool encrypt=false)
  11. {
  12. this.encrypt = encrypt;
  13. data = new List<KeyValuePair<string, object>>();
  14. file = new KeyValuePair<string, byte[]> ();
  15. }
  16. public void Add(string key, object value)
  17. {
  18. data.Add(new KeyValuePair<string, object>(key, value));
  19. }
  20. public void AddFile(string key, byte[] file)
  21. {
  22. this.file = new KeyValuePair<string, byte[]> (key, file);
  23. }
  24. public bool HasData()
  25. {
  26. return data.Count > 0 || !StringUtil.Empty(file.Key);
  27. }
  28. public string GetDataString()
  29. {
  30. string info = "";
  31. string result = "";
  32. if(encrypt)
  33. {
  34. JsonData json = JsonMapper.ToObject("{}");
  35. for(int i=0; i<data.Count; i++)
  36. {
  37. KeyValuePair<string, object> kvp = data[i];
  38. info += "["+kvp.Key+" : "+kvp.Value+"] ";
  39. json[kvp.Key] = kvp.Value.ToString();
  40. }
  41. result = "v="+DecryptionUtil.Encryption(json.ToJson());
  42. }
  43. else
  44. {
  45. string[] strArr = new string[data.Count];
  46. for(int i=0; i<data.Count; i++)
  47. {
  48. KeyValuePair<string, object> kvp = data[i];
  49. info += "["+kvp.Key+" : "+kvp.Value+"] ";
  50. strArr[i] = kvp.Key+"="+kvp.Value;
  51. }
  52. result = string.Join("&", strArr);
  53. }
  54. Debuger.Log("Url data : "+info);
  55. return result;
  56. }
  57. public WWWForm GetDataForm()
  58. {
  59. string info = "";
  60. WWWForm form = new WWWForm();
  61. if(encrypt)
  62. {
  63. JsonData json = JsonMapper.ToObject("{}");
  64. for(int i=0; i<data.Count; i++)
  65. {
  66. KeyValuePair<string, object> kvp = data[i];
  67. info += "["+kvp.Key+" : "+kvp.Value+"] ";
  68. json[kvp.Key] = kvp.Value.ToString();
  69. }
  70. form.AddField("v", DecryptionUtil.Encryption(json.ToJson()));
  71. }
  72. else
  73. {
  74. for(int i=0; i<data.Count; i++)
  75. {
  76. KeyValuePair<string, object> kvp = data[i];
  77. info += "["+kvp.Key+" : "+kvp.Value+"] ";
  78. form.AddField(kvp.Key, kvp.Value.ToString());
  79. }
  80. }
  81. if(!StringUtil.Empty(file.Key))
  82. {
  83. form.AddBinaryData (file.Key, file.Value);
  84. info += "[" + file.Key + " : bytes " + file.Value.Length + "]";
  85. }
  86. Debuger.Log("Url data : "+info);
  87. return form;
  88. }
  89. }