HttpHelper.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 int d;
  23. public static string Parse(string URL , out string ObjectURI)
  24. {
  25. int Pos;
  26. ObjectURI = null;
  27. if(StartsWithHttp(URL))
  28. {
  29. Pos = URL.IndexOf("/",7);
  30. }
  31. else
  32. {
  33. Pos = URL.IndexOf("/",0);
  34. }
  35. if(Pos>0 || Pos == 0)
  36. {
  37. ObjectURI = URL.Substring(Pos);
  38. return URL.Substring(0,Pos);
  39. }
  40. return URL;
  41. }
  42. public static bool StartsWithHttp(string URL)
  43. {
  44. if(URL.StartsWith("http://"))
  45. return true;
  46. else
  47. return false;
  48. }
  49. public static Stream CopyStream(Stream inStream)
  50. {
  51. Stream outStream = new MemoryStream();
  52. int temp;
  53. try
  54. {
  55. while(true)
  56. {
  57. temp = inStream.ReadByte();
  58. if(temp==-1)
  59. break;
  60. outStream.WriteByte((byte)temp);
  61. }
  62. outStream.Flush();
  63. outStream.Seek(0,SeekOrigin.Begin);
  64. }
  65. catch(Exception e)
  66. {
  67. Console.WriteLine(e);
  68. }
  69. return outStream;
  70. }
  71. public static bool CopyStream(Stream inStream,ref Stream outStream)
  72. {
  73. int temp;
  74. d++;
  75. try
  76. {
  77. FileStream f=null;
  78. if(d==2)
  79. f = new FileStream("f.txt",System.IO.FileMode.Create);
  80. while(true)
  81. {
  82. temp = inStream.ReadByte();
  83. if(d==2)
  84. f.WriteByte((byte)temp);
  85. if(temp==-1)
  86. break;
  87. outStream.WriteByte((byte)temp);
  88. }
  89. if(d==2)
  90. f.Close();
  91. outStream.Flush();
  92. if(outStream.CanSeek)
  93. outStream.Seek(0,SeekOrigin.Begin);
  94. }
  95. catch(Exception e)
  96. {
  97. Console.WriteLine(e);
  98. return false;
  99. }
  100. return true;
  101. }
  102. public static String GetHostName()
  103. {
  104. string hostName = Dns.GetHostName();
  105. if (hostName == null)
  106. {
  107. throw new ArgumentNullException("hostName");
  108. }
  109. return hostName;
  110. } // GetHostName
  111. public static String GetMachineName()
  112. {
  113. String machineName = GetHostName();
  114. if (machineName != null)
  115. machineName= Dns.GetHostByName(machineName).HostName;
  116. if (machineName== null)
  117. {
  118. throw new ArgumentNullException("machine");
  119. }
  120. return machineName;
  121. }
  122. public static String GetMachineIp()
  123. {
  124. String hostName = GetMachineName();
  125. String MachineIp=null;
  126. IPHostEntry Entries = Dns.GetHostByName(hostName);
  127. if ((Entries.AddressList.Length > 0)&&(Entries != null))
  128. {
  129. MachineIp = Entries.AddressList[0].ToString();
  130. }
  131. if (MachineIp == null)
  132. {
  133. throw new ArgumentNullException("ip");
  134. }
  135. return MachineIp;
  136. }
  137. }
  138. }