WebClient.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // System.Net.WebClient
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Specialized;
  10. using System.ComponentModel;
  11. using System.IO;
  12. using System.Runtime.InteropServices;
  13. using System.Runtime.Serialization;
  14. namespace System.Net
  15. {
  16. [ComVisible(true)]
  17. public sealed class WebClient : Component
  18. {
  19. ICredentials credentials;
  20. // Constructors
  21. public WebClient ()
  22. {
  23. }
  24. // Properties
  25. [MonoTODO]
  26. public string BaseAddress {
  27. get { throw new NotImplementedException (); }
  28. set { throw new NotImplementedException (); }
  29. }
  30. public ICredentials Credentials {
  31. get { return credentials; }
  32. set { credentials = value; }
  33. }
  34. [MonoTODO]
  35. public WebHeaderCollection Headers {
  36. get { throw new NotImplementedException (); }
  37. set { throw new NotImplementedException (); }
  38. }
  39. [MonoTODO]
  40. public NameValueCollection QueryString {
  41. get { throw new NotImplementedException (); }
  42. set { throw new NotImplementedException (); }
  43. }
  44. [MonoTODO]
  45. public WebHeaderCollection ResponseHeaders {
  46. get { throw new NotImplementedException (); }
  47. }
  48. // Methods
  49. public byte [] DownloadData (string address)
  50. {
  51. const int readSize = 8192;
  52. Stream networkStream = OpenRead (address);
  53. ArrayList chunks = new ArrayList ();
  54. byte[] buf = new byte [readSize];
  55. int size = 0;
  56. int total_size = 0;
  57. try {
  58. do {
  59. size = networkStream.Read (buf, 0, readSize);
  60. byte [] copy = new byte [size];
  61. Array.Copy (buf, 0, copy,0, size);
  62. chunks.Add (copy);
  63. total_size += size;
  64. } while (size != 0);
  65. } finally {
  66. networkStream.Close ();
  67. }
  68. byte [] result = new byte [total_size];
  69. int target = 0;
  70. foreach (byte [] block in chunks){
  71. int len = block.Length;
  72. Array.Copy (block, 0, result, target, len);
  73. target += len;
  74. }
  75. return result;
  76. }
  77. public void DownloadFile (string address, string fileName)
  78. {
  79. byte[] buf = DownloadData (address);
  80. using (FileStream f = new FileStream (fileName, FileMode.CreateNew)){
  81. f.Write (buf, 0, buf.Length);
  82. }
  83. }
  84. public Stream OpenRead (string address)
  85. {
  86. Uri uri = new Uri (address);
  87. WebRequest request = null;
  88. if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
  89. request = new HttpWebRequest (uri);
  90. else if(uri.Scheme == Uri.UriSchemeFile)
  91. request = new FileWebRequest (uri);
  92. else
  93. throw new NotImplementedException ();
  94. return request.GetResponse ().GetResponseStream ();
  95. }
  96. public Stream OpenWrite (string address)
  97. {
  98. return OpenWrite (address, "POST");
  99. }
  100. [MonoTODO]
  101. public Stream OpenWrite (string address, string method)
  102. {
  103. throw new NotImplementedException ();
  104. }
  105. public byte [] UploadData (string address, byte [] data)
  106. {
  107. return UploadData (address, "POST", data);
  108. }
  109. [MonoTODO]
  110. public byte [] UploadData (string address, string method, byte [] data)
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. public byte [] UploadFile (string address, string fileName)
  115. {
  116. return UploadFile (address, "POST", fileName);
  117. }
  118. [MonoTODO]
  119. public byte[] UploadFile (string address, string method, string fileName)
  120. {
  121. throw new NotImplementedException ();
  122. }
  123. public byte[] UploadValues (string address, NameValueCollection data)
  124. {
  125. return UploadValues (address, "POST", data);
  126. }
  127. [MonoTODO]
  128. public byte[] UploadValues (string address, string method, NameValueCollection data)
  129. {
  130. throw new NotImplementedException ();
  131. }
  132. }
  133. }