MultipartForm.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Net;
  30. using System.IO;
  31. using System.Text;
  32. namespace OpenSim.Framework
  33. {
  34. public static class MultipartForm
  35. {
  36. #region Helper Classes
  37. public abstract class Element
  38. {
  39. public string Name;
  40. }
  41. public class File : Element
  42. {
  43. public string Filename;
  44. public string ContentType;
  45. public byte[] Data;
  46. public File(string name, string filename, string contentType, byte[] data)
  47. {
  48. Name = name;
  49. Filename = filename;
  50. ContentType = contentType;
  51. Data = data;
  52. }
  53. }
  54. public class Parameter : Element
  55. {
  56. public string Value;
  57. public Parameter(string name, string value)
  58. {
  59. Name = name;
  60. Value = value;
  61. }
  62. }
  63. #endregion Helper Classes
  64. public static HttpWebResponse Post(HttpWebRequest request, List<Element> postParameters)
  65. {
  66. string boundary = Boundary();
  67. // Set up the request properties
  68. request.Method = "POST";
  69. request.ContentType = "multipart/form-data; boundary=" + boundary;
  70. #region Stream Writing
  71. using (MemoryStream formDataStream = new MemoryStream())
  72. {
  73. foreach (var param in postParameters)
  74. {
  75. if (param is File)
  76. {
  77. File file = (File)param;
  78. // Add just the first part of this param, since we will write the file data directly to the Stream
  79. string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
  80. boundary,
  81. file.Name,
  82. !String.IsNullOrEmpty(file.Filename) ? file.Filename : "tempfile",
  83. file.ContentType);
  84. formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, header.Length);
  85. formDataStream.Write(file.Data, 0, file.Data.Length);
  86. }
  87. else
  88. {
  89. Parameter parameter = (Parameter)param;
  90. string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n",
  91. boundary,
  92. parameter.Name,
  93. parameter.Value);
  94. formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, postData.Length);
  95. }
  96. }
  97. // Add the end of the request
  98. byte[] footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
  99. formDataStream.Write(footer, 0, footer.Length);
  100. request.ContentLength = formDataStream.Length;
  101. // Copy the temporary stream to the network stream
  102. formDataStream.Seek(0, SeekOrigin.Begin);
  103. using (Stream requestStream = request.GetRequestStream())
  104. formDataStream.CopyStream(requestStream, (int)formDataStream.Length);
  105. }
  106. #endregion Stream Writing
  107. return request.GetResponse() as HttpWebResponse;
  108. }
  109. private static string Boundary()
  110. {
  111. Random rnd = new Random();
  112. string formDataBoundary = String.Empty;
  113. while (formDataBoundary.Length < 15)
  114. formDataBoundary = formDataBoundary + rnd.Next();
  115. formDataBoundary = formDataBoundary.Substring(0, 15);
  116. formDataBoundary = "-----------------------------" + formDataBoundary;
  117. return formDataBoundary;
  118. }
  119. }
  120. }