UnityForm.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #if !BESTHTTP_DISABLE_UNITY_FORM
  2. using UnityEngine;
  3. namespace BestHTTP.Forms
  4. {
  5. /// <summary>
  6. /// For backward compatibility.
  7. /// </summary>
  8. public sealed class UnityForm : HTTPFormBase
  9. {
  10. public WWWForm Form { get; set; }
  11. public UnityForm()
  12. {
  13. }
  14. public UnityForm(WWWForm form)
  15. {
  16. Form = form;
  17. }
  18. public override void CopyFrom(HTTPFormBase fields)
  19. {
  20. this.Fields = fields.Fields;
  21. this.IsChanged = true;
  22. if (Form == null)
  23. {
  24. Form = new WWWForm();
  25. if (Fields != null)
  26. for (int i = 0; i < Fields.Count; ++i)
  27. {
  28. var field = Fields[i];
  29. if (string.IsNullOrEmpty(field.Text) && field.Binary != null)
  30. Form.AddBinaryData(field.Name, field.Binary, field.FileName, field.MimeType);
  31. else
  32. Form.AddField(field.Name, field.Text, field.Encoding);
  33. }
  34. }
  35. }
  36. public override void PrepareRequest(HTTPRequest request)
  37. {
  38. if (Form.headers.ContainsKey("Content-Type"))
  39. request.SetHeader("Content-Type", Form.headers["Content-Type"] as string);
  40. else
  41. request.SetHeader("Content-Type", "application/x-www-form-urlencoded");
  42. }
  43. public override byte[] GetData()
  44. {
  45. return Form.data;
  46. }
  47. }
  48. }
  49. #endif