HttpHelper.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //==========================================================================
  2. // File: HttpHelper.cs
  3. //
  4. // Summary: Implements Helper internal class for transmission over HTTP.
  5. //
  6. // Classes: internal HttpHelper
  7. //
  8. // By :
  9. // Ahmad Tantawy [email protected]
  10. // Ahmad Kadry [email protected]
  11. // Hussein Mehanna [email protected]
  12. //
  13. //==========================================================================
  14. using System;
  15. using System.IO;
  16. using System.Net;
  17. using System.Text.RegularExpressions;
  18. namespace System.Runtime.Remoting.Channels.Http
  19. {
  20. internal class HttpHelper
  21. {
  22. public static string Parse(string URL , out string ObjectURI)
  23. {
  24. int Pos;
  25. ObjectURI = null;
  26. string ChannelURI = null;
  27. if(StartsWithHttp(URL))
  28. {
  29. Pos = URL.IndexOf("/",7);
  30. if(Pos >= 0)
  31. {
  32. ObjectURI = URL.Substring(Pos);
  33. ChannelURI = URL.Substring(0, Pos);
  34. }
  35. else ChannelURI = URL;
  36. }
  37. return ChannelURI;
  38. }
  39. public static bool StartsWithHttp(string URL)
  40. {
  41. if(URL.StartsWith("http://"))
  42. return true;
  43. else
  44. return false;
  45. }
  46. public static Stream CopyStream(Stream inStream)
  47. {
  48. Stream outStream = new MemoryStream();
  49. int temp;
  50. try
  51. {
  52. while(true)
  53. {
  54. temp = inStream.ReadByte();
  55. if(temp==-1)
  56. break;
  57. outStream.WriteByte((byte)temp);
  58. }
  59. outStream.Flush();
  60. outStream.Seek(0,SeekOrigin.Begin);
  61. }
  62. catch(Exception e)
  63. {
  64. Console.WriteLine(e);
  65. }
  66. return outStream;
  67. }
  68. public static bool CopyStream(Stream inStream, Stream outStream)
  69. {
  70. int temp;
  71. try
  72. {
  73. while(true)
  74. {
  75. temp = inStream.ReadByte();
  76. if(temp==-1)
  77. break;
  78. outStream.WriteByte((byte)temp);
  79. }
  80. outStream.Flush();
  81. if(outStream.CanSeek)
  82. outStream.Seek(0,SeekOrigin.Begin);
  83. }
  84. catch(Exception e)
  85. {
  86. Console.WriteLine(e);
  87. return false;
  88. }
  89. return true;
  90. }
  91. public static String GetHostName()
  92. {
  93. string hostName = Dns.GetHostName();
  94. if (hostName == null)
  95. {
  96. throw new ArgumentNullException("hostName");
  97. }
  98. return hostName;
  99. } // GetHostName
  100. public static String GetMachineName()
  101. {
  102. String machineName = GetHostName();
  103. if (machineName != null)
  104. machineName= Dns.GetHostByName(machineName).HostName;
  105. if (machineName== null)
  106. {
  107. throw new ArgumentNullException("machine");
  108. }
  109. return machineName;
  110. }
  111. }
  112. }