UrlEncodedParameterWriter.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // System.Web.Services.Protocols.UrlEncodedParameterWriter.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.IO;
  31. using System.Text;
  32. using System.Web.Services;
  33. using System.Web;
  34. using System.Reflection;
  35. namespace System.Web.Services.Protocols {
  36. public abstract class UrlEncodedParameterWriter : MimeParameterWriter {
  37. Encoding requestEncoding;
  38. ParameterInfo[] parameters;
  39. #region Constructors
  40. protected UrlEncodedParameterWriter ()
  41. {
  42. }
  43. #endregion // Constructors
  44. #region Properties
  45. public override Encoding RequestEncoding {
  46. get { return requestEncoding; }
  47. set { requestEncoding = value; }
  48. }
  49. #endregion // Properties
  50. #region Methods
  51. protected void Encode (TextWriter writer, object[] values)
  52. {
  53. for (int n=0; n<values.Length; n++)
  54. {
  55. if (n>0) writer.Write ("&");
  56. Encode (writer, parameters[n].Name, values[n]);
  57. }
  58. }
  59. protected void Encode (TextWriter writer, string name, object value)
  60. {
  61. if (requestEncoding != null)
  62. {
  63. writer.Write (HttpUtility.UrlEncode (name, requestEncoding));
  64. writer.Write ("=");
  65. writer.Write (HttpUtility.UrlEncode (value.ToString(), requestEncoding));
  66. }
  67. else
  68. {
  69. writer.Write (HttpUtility.UrlEncode (name));
  70. writer.Write ("=");
  71. writer.Write (HttpUtility.UrlEncode (value.ToString()));
  72. }
  73. }
  74. public override object GetInitializer (LogicalMethodInfo methodInfo)
  75. {
  76. if (methodInfo.OutParameters.Length > 0) return null;
  77. else return methodInfo.Parameters;
  78. }
  79. public override void Initialize (object initializer)
  80. {
  81. parameters = (ParameterInfo[]) initializer;
  82. }
  83. #endregion // Methods
  84. }
  85. }