WebClient.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. //
  2. // System.Net.WebClient
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Atsushi Enomoto ([email protected])
  8. // Miguel de Icaza ([email protected])
  9. //
  10. // Copyright 2003 Ximian, Inc. (http://www.ximian.com)
  11. // Copyright 2006, 2007 Novell, Inc. (http://www.novell.com)
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. //
  34. // Notes on CancelAsync and Async methods:
  35. //
  36. // WebClient.CancelAsync is implemented by calling Thread.Interrupt
  37. // in our helper thread. The various async methods have to cancel
  38. // any ongoing requests by calling request.Abort () at that point.
  39. // In a few places (UploadDataCore, UploadValuesCore,
  40. // UploadFileCore) we catch the ThreadInterruptedException and
  41. // abort the request there.
  42. //
  43. // Higher level routines (the async callbacks) also need to catch
  44. // the exception and raise the OnXXXXCompleted events there with
  45. // the "canceled" flag set to true.
  46. //
  47. // In a few other places where these helper routines are not used
  48. // (OpenReadAsync for example) catching the ThreadAbortException
  49. // also must abort the request.
  50. //
  51. // The Async methods currently differ in their implementation from
  52. // the .NET implementation in that we manually catch any other
  53. // exceptions and correctly raise the OnXXXXCompleted passing the
  54. // Exception that caused the problem. The .NET implementation
  55. // does not seem to have a mechanism to flag errors that happen
  56. // during downloads though. We do this because we still need to
  57. // catch the exception on these helper threads, or we would
  58. // otherwise kill the application (on the 2.x profile, uncaught
  59. // exceptions in threads terminate the application).
  60. //
  61. using System;
  62. using System.Collections.Specialized;
  63. using System.ComponentModel;
  64. using System.IO;
  65. using System.Runtime.InteropServices;
  66. using System.Runtime.Serialization;
  67. using System.Text;
  68. using System.Threading;
  69. #if NET_2_0
  70. using System.Net.Cache;
  71. #endif
  72. namespace System.Net
  73. {
  74. [ComVisible(true)]
  75. public
  76. #if !NET_2_0
  77. sealed
  78. #endif
  79. class WebClient : Component
  80. {
  81. static readonly string urlEncodedCType = "application/x-www-form-urlencoded";
  82. static byte [] hexBytes;
  83. ICredentials credentials;
  84. WebHeaderCollection headers;
  85. WebHeaderCollection responseHeaders;
  86. Uri baseAddress;
  87. string baseString;
  88. NameValueCollection queryString;
  89. bool is_busy;
  90. #if NET_2_0
  91. bool async;
  92. Thread async_thread;
  93. Encoding encoding = Encoding.Default;
  94. IWebProxy proxy;
  95. #endif
  96. // Constructors
  97. static WebClient ()
  98. {
  99. hexBytes = new byte [16];
  100. int index = 0;
  101. for (int i = '0'; i <= '9'; i++, index++)
  102. hexBytes [index] = (byte) i;
  103. for (int i = 'a'; i <= 'f'; i++, index++)
  104. hexBytes [index] = (byte) i;
  105. }
  106. public WebClient ()
  107. {
  108. }
  109. // Properties
  110. public string BaseAddress {
  111. get {
  112. if (baseString == null) {
  113. if (baseAddress == null)
  114. return string.Empty;
  115. }
  116. baseString = baseAddress.ToString ();
  117. return baseString;
  118. }
  119. set {
  120. if (value == null || value.Length == 0) {
  121. baseAddress = null;
  122. } else {
  123. baseAddress = new Uri (value);
  124. }
  125. }
  126. }
  127. #if NET_2_0
  128. static Exception GetMustImplement ()
  129. {
  130. return new NotImplementedException ();
  131. }
  132. [MonoTODO]
  133. public RequestCachePolicy CachePolicy
  134. {
  135. get {
  136. throw GetMustImplement ();
  137. }
  138. set {
  139. throw GetMustImplement ();
  140. }
  141. }
  142. [MonoTODO]
  143. public bool UseDefaultCredentials
  144. {
  145. get {
  146. throw GetMustImplement ();
  147. }
  148. set {
  149. throw GetMustImplement ();
  150. }
  151. }
  152. #endif
  153. public ICredentials Credentials {
  154. get { return credentials; }
  155. set { credentials = value; }
  156. }
  157. public WebHeaderCollection Headers {
  158. get {
  159. if (headers == null)
  160. headers = new WebHeaderCollection ();
  161. return headers;
  162. }
  163. set { headers = value; }
  164. }
  165. public NameValueCollection QueryString {
  166. get {
  167. if (queryString == null)
  168. queryString = new NameValueCollection ();
  169. return queryString;
  170. }
  171. set { queryString = value; }
  172. }
  173. public WebHeaderCollection ResponseHeaders {
  174. get { return responseHeaders; }
  175. }
  176. #if NET_2_0
  177. public Encoding Encoding {
  178. get { return encoding; }
  179. set {
  180. if (value == null)
  181. throw new ArgumentNullException ("Encoding");
  182. encoding = value;
  183. }
  184. }
  185. public IWebProxy Proxy {
  186. get { return proxy; }
  187. set { proxy = value; }
  188. }
  189. #endif
  190. #if NET_2_0
  191. public bool IsBusy {
  192. get { return is_busy; }
  193. }
  194. #else
  195. bool IsBusy {
  196. get { return is_busy; }
  197. }
  198. #endif
  199. // Methods
  200. void CheckBusy ()
  201. {
  202. if (IsBusy)
  203. throw new NotSupportedException ("WebClient does not support conccurent I/O operations.");
  204. }
  205. void SetBusy ()
  206. {
  207. lock (this) {
  208. CheckBusy ();
  209. is_busy = true;
  210. }
  211. }
  212. // DownloadData
  213. public byte [] DownloadData (string address)
  214. {
  215. #if NET_2_0
  216. if (address == null)
  217. throw new ArgumentNullException ("address");
  218. #endif
  219. return DownloadData (CreateUri (address));
  220. }
  221. #if NET_2_0
  222. public
  223. #endif
  224. byte [] DownloadData (Uri address)
  225. {
  226. #if NET_2_0
  227. if (address == null)
  228. throw new ArgumentNullException ("address");
  229. #endif
  230. try {
  231. SetBusy ();
  232. #if NET_2_0
  233. async = false;
  234. #endif
  235. return DownloadDataCore (address, null);
  236. } finally {
  237. is_busy = false;
  238. }
  239. }
  240. byte [] DownloadDataCore (Uri address, object userToken)
  241. {
  242. WebRequest request = null;
  243. try {
  244. request = SetupRequest (address);
  245. WebResponse response = GetWebResponse (request);
  246. Stream st = response.GetResponseStream ();
  247. return ReadAll (st, (int) response.ContentLength, userToken);
  248. } catch (ThreadInterruptedException){
  249. if (request != null)
  250. request.Abort ();
  251. throw;
  252. } catch (WebException wexc) {
  253. throw;
  254. } catch (Exception ex) {
  255. throw new WebException ("An error occurred " +
  256. "performing a WebClient request.", ex);
  257. }
  258. }
  259. // DownloadFile
  260. public void DownloadFile (string address, string fileName)
  261. {
  262. #if NET_2_0
  263. if (address == null)
  264. throw new ArgumentNullException ("address");
  265. #endif
  266. DownloadFile (CreateUri (address), fileName);
  267. }
  268. #if NET_2_0
  269. public
  270. #endif
  271. void DownloadFile (Uri address, string fileName)
  272. {
  273. #if NET_2_0
  274. if (address == null)
  275. throw new ArgumentNullException ("address");
  276. if (fileName == null)
  277. throw new ArgumentNullException ("fileName");
  278. #endif
  279. try {
  280. SetBusy ();
  281. #if NET_2_0
  282. async = false;
  283. #endif
  284. DownloadFileCore (address, fileName, null);
  285. } catch (WebException wexc) {
  286. throw;
  287. } catch (Exception ex) {
  288. throw new WebException ("An error occurred " +
  289. "performing a WebClient request.", ex);
  290. } finally {
  291. is_busy = false;
  292. }
  293. }
  294. void DownloadFileCore (Uri address, string fileName, object userToken)
  295. {
  296. WebRequest request = null;
  297. using (FileStream f = new FileStream (fileName, FileMode.Create)) {
  298. try {
  299. request = SetupRequest (address);
  300. WebResponse response = GetWebResponse (request);
  301. Stream st = response.GetResponseStream ();
  302. int cLength = (int) response.ContentLength;
  303. int length = (cLength <= -1 || cLength > 32*1024) ? 32*1024 : cLength;
  304. byte [] buffer = new byte [length];
  305. int nread = 0;
  306. #if NET_2_0
  307. long notify_total = 0;
  308. #endif
  309. while ((nread = st.Read (buffer, 0, length)) != 0){
  310. #if NET_2_0
  311. if (async){
  312. notify_total += nread;
  313. OnDownloadProgressChanged (
  314. new DownloadProgressChangedEventArgs (notify_total, response.ContentLength, userToken));
  315. }
  316. #endif
  317. f.Write (buffer, 0, nread);
  318. }
  319. } catch (ThreadInterruptedException){
  320. if (request != null)
  321. request.Abort ();
  322. throw;
  323. }
  324. }
  325. }
  326. // OpenRead
  327. public Stream OpenRead (string address)
  328. {
  329. #if NET_2_0
  330. if (address == null)
  331. throw new ArgumentNullException ("address");
  332. #endif
  333. return OpenRead (CreateUri (address));
  334. }
  335. #if NET_2_0
  336. public
  337. #endif
  338. Stream OpenRead (Uri address)
  339. {
  340. #if NET_2_0
  341. if (address == null)
  342. throw new ArgumentNullException ("address");
  343. #endif
  344. WebRequest request = null;
  345. try {
  346. SetBusy ();
  347. #if NET_2_0
  348. async = false;
  349. #endif
  350. request = SetupRequest (address);
  351. WebResponse response = GetWebResponse (request);
  352. return response.GetResponseStream ();
  353. } catch (WebException wexc) {
  354. throw;
  355. } catch (Exception ex) {
  356. throw new WebException ("An error occurred " +
  357. "performing a WebClient request.", ex);
  358. } finally {
  359. is_busy = false;
  360. }
  361. }
  362. // OpenWrite
  363. public Stream OpenWrite (string address)
  364. {
  365. #if NET_2_0
  366. if (address == null)
  367. throw new ArgumentNullException ("address");
  368. #endif
  369. return OpenWrite (CreateUri (address));
  370. }
  371. public Stream OpenWrite (string address, string method)
  372. {
  373. #if NET_2_0
  374. if (address == null)
  375. throw new ArgumentNullException ("address");
  376. #endif
  377. return OpenWrite (CreateUri (address), method);
  378. }
  379. #if NET_2_0
  380. public
  381. #endif
  382. Stream OpenWrite (Uri address)
  383. {
  384. return OpenWrite (address, (string) null);
  385. }
  386. #if NET_2_0
  387. public
  388. #endif
  389. Stream OpenWrite (Uri address, string method)
  390. {
  391. #if NET_2_0
  392. if (address == null)
  393. throw new ArgumentNullException ("address");
  394. #endif
  395. try {
  396. SetBusy ();
  397. #if NET_2_0
  398. async = false;
  399. #endif
  400. WebRequest request = SetupRequest (address, method, true);
  401. return request.GetRequestStream ();
  402. } catch (WebException wexc) {
  403. throw;
  404. } catch (Exception ex) {
  405. throw new WebException ("An error occurred " +
  406. "performing a WebClient request.", ex);
  407. } finally {
  408. is_busy = false;
  409. }
  410. }
  411. private string DetermineMethod (Uri address, string method, bool is_upload)
  412. {
  413. if (method != null)
  414. return method;
  415. #if NET_2_0
  416. if (address.Scheme == Uri.UriSchemeFtp)
  417. return (is_upload) ? "STOR" : "RETR";
  418. #endif
  419. return (is_upload) ? "POST" : "GET";
  420. }
  421. // UploadData
  422. public byte [] UploadData (string address, byte [] data)
  423. {
  424. #if NET_2_0
  425. if (address == null)
  426. throw new ArgumentNullException ("address");
  427. #endif
  428. return UploadData (CreateUri (address), data);
  429. }
  430. public byte [] UploadData (string address, string method, byte [] data)
  431. {
  432. #if NET_2_0
  433. if (address == null)
  434. throw new ArgumentNullException ("address");
  435. #endif
  436. return UploadData (CreateUri (address), method, data);
  437. }
  438. #if NET_2_0
  439. public
  440. #endif
  441. byte [] UploadData (Uri address, byte [] data)
  442. {
  443. return UploadData (address, (string) null, data);
  444. }
  445. #if NET_2_0
  446. public
  447. #endif
  448. byte [] UploadData (Uri address, string method, byte [] data)
  449. {
  450. #if NET_2_0
  451. if (address == null)
  452. throw new ArgumentNullException ("address");
  453. if (data == null)
  454. throw new ArgumentNullException ("data");
  455. #endif
  456. try {
  457. SetBusy ();
  458. #if NET_2_0
  459. async = false;
  460. #endif
  461. return UploadDataCore (address, method, data, null);
  462. } catch (WebException) {
  463. throw;
  464. } catch (Exception ex) {
  465. throw new WebException ("An error occurred " +
  466. "performing a WebClient request.", ex);
  467. } finally {
  468. is_busy = false;
  469. }
  470. }
  471. byte [] UploadDataCore (Uri address, string method, byte [] data, object userToken)
  472. {
  473. #if ONLY_1_1
  474. if (address == null)
  475. throw new ArgumentNullException ("address");
  476. if (data == null)
  477. throw new ArgumentNullException ("data");
  478. #endif
  479. WebRequest request = SetupRequest (address, method, true);
  480. try {
  481. int contentLength = data.Length;
  482. request.ContentLength = contentLength;
  483. using (Stream stream = request.GetRequestStream ()) {
  484. stream.Write (data, 0, contentLength);
  485. }
  486. WebResponse response = GetWebResponse (request);
  487. Stream st = response.GetResponseStream ();
  488. return ReadAll (st, (int) response.ContentLength, userToken);
  489. } catch (ThreadInterruptedException){
  490. if (request != null)
  491. request.Abort ();
  492. throw;
  493. }
  494. }
  495. // UploadFile
  496. public byte [] UploadFile (string address, string fileName)
  497. {
  498. #if NET_2_0
  499. if (address == null)
  500. throw new ArgumentNullException ("address");
  501. #endif
  502. return UploadFile (CreateUri (address), fileName);
  503. }
  504. #if NET_2_0
  505. public
  506. #endif
  507. byte [] UploadFile (Uri address, string fileName)
  508. {
  509. return UploadFile (address, (string) null, fileName);
  510. }
  511. public byte [] UploadFile (string address, string method, string fileName)
  512. {
  513. return UploadFile (CreateUri (address), method, fileName);
  514. }
  515. #if NET_2_0
  516. public
  517. #endif
  518. byte [] UploadFile (Uri address, string method, string fileName)
  519. {
  520. #if NET_2_0
  521. if (address == null)
  522. throw new ArgumentNullException ("address");
  523. if (fileName == null)
  524. throw new ArgumentNullException ("fileName");
  525. #endif
  526. try {
  527. SetBusy ();
  528. #if NET_2_0
  529. async = false;
  530. #endif
  531. return UploadFileCore (address, method, fileName, null);
  532. } catch (WebException wexc) {
  533. throw;
  534. } catch (Exception ex) {
  535. throw new WebException ("An error occurred " +
  536. "performing a WebClient request.", ex);
  537. } finally {
  538. is_busy = false;
  539. }
  540. }
  541. byte [] UploadFileCore (Uri address, string method, string fileName, object userToken)
  542. {
  543. #if ONLY_1_1
  544. if (address == null)
  545. throw new ArgumentNullException ("address");
  546. #endif
  547. string fileCType = Headers ["Content-Type"];
  548. if (fileCType != null) {
  549. string lower = fileCType.ToLower ();
  550. if (lower.StartsWith ("multipart/"))
  551. throw new WebException ("Content-Type cannot be set to a multipart" +
  552. " type for this request.");
  553. } else {
  554. fileCType = "application/octet-stream";
  555. }
  556. string boundary = "------------" + DateTime.Now.Ticks.ToString ("x");
  557. Headers ["Content-Type"] = String.Format ("multipart/form-data; boundary={0}", boundary);
  558. Stream reqStream = null;
  559. Stream fStream = null;
  560. byte [] resultBytes = null;
  561. fileName = Path.GetFullPath (fileName);
  562. WebRequest request = null;
  563. try {
  564. fStream = File.OpenRead (fileName);
  565. request = SetupRequest (address, method, true);
  566. reqStream = request.GetRequestStream ();
  567. byte [] realBoundary = Encoding.ASCII.GetBytes ("--" + boundary + "\r\n");
  568. reqStream.Write (realBoundary, 0, realBoundary.Length);
  569. string partHeaders = String.Format ("Content-Disposition: form-data; " +
  570. "name=\"file\"; filename=\"{0}\"\r\n" +
  571. "Content-Type: {1}\r\n\r\n",
  572. Path.GetFileName (fileName), fileCType);
  573. byte [] partHeadersBytes = Encoding.UTF8.GetBytes (partHeaders);
  574. reqStream.Write (partHeadersBytes, 0, partHeadersBytes.Length);
  575. int nread;
  576. byte [] buffer = new byte [4096];
  577. while ((nread = fStream.Read (buffer, 0, 4096)) != 0)
  578. reqStream.Write (buffer, 0, nread);
  579. reqStream.WriteByte ((byte) '\r');
  580. reqStream.WriteByte ((byte) '\n');
  581. reqStream.Write (realBoundary, 0, realBoundary.Length);
  582. reqStream.Close ();
  583. reqStream = null;
  584. WebResponse response = GetWebResponse (request);
  585. Stream st = response.GetResponseStream ();
  586. resultBytes = ReadAll (st, (int) response.ContentLength, userToken);
  587. } catch (ThreadInterruptedException){
  588. if (request != null)
  589. request.Abort ();
  590. throw;
  591. } finally {
  592. if (fStream != null)
  593. fStream.Close ();
  594. if (reqStream != null)
  595. reqStream.Close ();
  596. }
  597. return resultBytes;
  598. }
  599. public byte[] UploadValues (string address, NameValueCollection data)
  600. {
  601. #if NET_2_0
  602. if (address == null)
  603. throw new ArgumentNullException ("address");
  604. #endif
  605. return UploadValues (CreateUri (address), data);
  606. }
  607. public byte[] UploadValues (string address, string method, NameValueCollection data)
  608. {
  609. #if NET_2_0
  610. if (address == null)
  611. throw new ArgumentNullException ("address");
  612. #endif
  613. return UploadValues (CreateUri (address), method, data);
  614. }
  615. #if NET_2_0
  616. public
  617. #endif
  618. byte[] UploadValues (Uri address, NameValueCollection data)
  619. {
  620. return UploadValues (address, (string) null, data);
  621. }
  622. #if NET_2_0
  623. public
  624. #endif
  625. byte[] UploadValues (Uri address, string method, NameValueCollection data)
  626. {
  627. #if NET_2_0
  628. if (address == null)
  629. throw new ArgumentNullException ("address");
  630. if (data == null)
  631. throw new ArgumentNullException ("data");
  632. #endif
  633. try {
  634. SetBusy ();
  635. #if NET_2_0
  636. async = false;
  637. #endif
  638. return UploadValuesCore (address, method, data, null);
  639. } catch (WebException wexc) {
  640. throw;
  641. } catch (Exception ex) {
  642. throw new WebException ("An error occurred " +
  643. "performing a WebClient request.", ex);
  644. } finally {
  645. is_busy = false;
  646. }
  647. }
  648. byte[] UploadValuesCore (Uri uri, string method, NameValueCollection data, object userToken)
  649. {
  650. #if ONLY_1_1
  651. if (data == null)
  652. throw new ArgumentNullException ("data");
  653. #endif
  654. string cType = Headers ["Content-Type"];
  655. if (cType != null && String.Compare (cType, urlEncodedCType, true) != 0)
  656. throw new WebException ("Content-Type header cannot be changed from its default " +
  657. "value for this request.");
  658. Headers ["Content-Type"] = urlEncodedCType;
  659. WebRequest request = SetupRequest (uri, method, true);
  660. try {
  661. MemoryStream tmpStream = new MemoryStream ();
  662. foreach (string key in data) {
  663. byte [] bytes = Encoding.UTF8.GetBytes (key);
  664. UrlEncodeAndWrite (tmpStream, bytes);
  665. tmpStream.WriteByte ((byte) '=');
  666. bytes = Encoding.UTF8.GetBytes (data [key]);
  667. UrlEncodeAndWrite (tmpStream, bytes);
  668. tmpStream.WriteByte ((byte) '&');
  669. }
  670. int length = (int) tmpStream.Length;
  671. if (length > 0)
  672. tmpStream.SetLength (--length); // remove trailing '&'
  673. byte [] buf = tmpStream.GetBuffer ();
  674. request.ContentLength = length;
  675. using (Stream rqStream = request.GetRequestStream ()) {
  676. rqStream.Write (buf, 0, length);
  677. }
  678. tmpStream.Close ();
  679. WebResponse response = GetWebResponse (request);
  680. Stream st = response.GetResponseStream ();
  681. return ReadAll (st, (int) response.ContentLength, userToken);
  682. } catch (ThreadInterruptedException) {
  683. request.Abort ();
  684. throw;
  685. }
  686. }
  687. #if NET_2_0
  688. public string DownloadString (string address)
  689. {
  690. if (address == null)
  691. throw new ArgumentNullException ("address");
  692. return encoding.GetString (DownloadData (CreateUri (address)));
  693. }
  694. public string DownloadString (Uri address)
  695. {
  696. if (address == null)
  697. throw new ArgumentNullException ("address");
  698. return encoding.GetString (DownloadData (CreateUri (address)));
  699. }
  700. public string UploadString (string address, string data)
  701. {
  702. if (address == null)
  703. throw new ArgumentNullException ("address");
  704. if (data == null)
  705. throw new ArgumentNullException ("data");
  706. byte [] resp = UploadData (address, encoding.GetBytes (data));
  707. return encoding.GetString (resp);
  708. }
  709. public string UploadString (string address, string method, string data)
  710. {
  711. if (address == null)
  712. throw new ArgumentNullException ("address");
  713. if (data == null)
  714. throw new ArgumentNullException ("data");
  715. byte [] resp = UploadData (address, method, encoding.GetBytes (data));
  716. return encoding.GetString (resp);
  717. }
  718. public string UploadString (Uri address, string data)
  719. {
  720. if (address == null)
  721. throw new ArgumentNullException ("address");
  722. if (data == null)
  723. throw new ArgumentNullException ("data");
  724. byte [] resp = UploadData (address, encoding.GetBytes (data));
  725. return encoding.GetString (resp);
  726. }
  727. public string UploadString (Uri address, string method, string data)
  728. {
  729. if (address == null)
  730. throw new ArgumentNullException ("address");
  731. if (data == null)
  732. throw new ArgumentNullException ("data");
  733. byte [] resp = UploadData (address, method, encoding.GetBytes (data));
  734. return encoding.GetString (resp);
  735. }
  736. public event DownloadDataCompletedEventHandler DownloadDataCompleted;
  737. public event AsyncCompletedEventHandler DownloadFileCompleted;
  738. public event DownloadProgressChangedEventHandler DownloadProgressChanged;
  739. public event DownloadStringCompletedEventHandler DownloadStringCompleted;
  740. public event OpenReadCompletedEventHandler OpenReadCompleted;
  741. public event OpenWriteCompletedEventHandler OpenWriteCompleted;
  742. public event UploadDataCompletedEventHandler UploadDataCompleted;
  743. public event UploadFileCompletedEventHandler UploadFileCompleted;
  744. public event UploadProgressChangedEventHandler UploadProgressChanged;
  745. public event UploadStringCompletedEventHandler UploadStringCompleted;
  746. public event UploadValuesCompletedEventHandler UploadValuesCompleted;
  747. #endif
  748. Uri CreateUri (string address)
  749. {
  750. #if ONLY_1_1
  751. try {
  752. return MakeUri (address);
  753. } catch (Exception ex) {
  754. throw new WebException ("An error occurred " +
  755. "performing a WebClient request.", ex);
  756. }
  757. #else
  758. return MakeUri (address);
  759. #endif
  760. }
  761. #if NET_2_0
  762. Uri CreateUri (Uri address)
  763. {
  764. string query = address.Query;
  765. if (String.IsNullOrEmpty (query))
  766. query = GetQueryString (true);
  767. if (baseAddress == null && query == null)
  768. return address;
  769. if (baseAddress == null)
  770. return new Uri (address.ToString () + query, (query != null));
  771. if (query == null)
  772. return new Uri (baseAddress, address.ToString ());
  773. return new Uri (baseAddress, address.ToString () + query, (query != null));
  774. }
  775. #endif
  776. string GetQueryString (bool add_qmark)
  777. {
  778. if (queryString == null || queryString.Count == 0)
  779. return null;
  780. StringBuilder sb = new StringBuilder ();
  781. if (add_qmark)
  782. sb.Append ('?');
  783. foreach (string key in queryString)
  784. sb.AppendFormat ("{0}={1}&", key, UrlEncode (queryString [key]));
  785. if (sb.Length != 0)
  786. sb.Length--; // removes last '&' or the '?' if empty.
  787. if (sb.Length == 0)
  788. return null;
  789. return sb.ToString ();
  790. }
  791. Uri MakeUri (string path)
  792. {
  793. string query = GetQueryString (true);
  794. if (baseAddress == null && query == null) {
  795. try {
  796. return new Uri (path);
  797. #if NET_2_0
  798. } catch (ArgumentNullException) {
  799. path = Path.GetFullPath (path);
  800. return new Uri ("file://" + path);
  801. #endif
  802. } catch (UriFormatException) {
  803. path = Path.GetFullPath (path);
  804. return new Uri ("file://" + path);
  805. }
  806. }
  807. if (baseAddress == null)
  808. return new Uri (path + query, (query != null));
  809. if (query == null)
  810. return new Uri (baseAddress, path);
  811. return new Uri (baseAddress, path + query, (query != null));
  812. }
  813. WebRequest SetupRequest (Uri uri)
  814. {
  815. WebRequest request = WebRequest.Create (uri);
  816. #if NET_2_0
  817. if (Proxy != null)
  818. request.Proxy = Proxy;
  819. #endif
  820. request.Credentials = credentials;
  821. // Special headers. These are properties of HttpWebRequest.
  822. // What do we do with other requests differnt from HttpWebRequest?
  823. if (headers != null && headers.Count != 0 && (request is HttpWebRequest)) {
  824. HttpWebRequest req = (HttpWebRequest) request;
  825. string expect = headers ["Expect"];
  826. string contentType = headers ["Content-Type"];
  827. string accept = headers ["Accept"];
  828. string connection = headers ["Connection"];
  829. string userAgent = headers ["User-Agent"];
  830. string referer = headers ["Referer"];
  831. headers.RemoveInternal ("Expect");
  832. headers.RemoveInternal ("Content-Type");
  833. headers.RemoveInternal ("Accept");
  834. headers.RemoveInternal ("Connection");
  835. headers.RemoveInternal ("Referer");
  836. headers.RemoveInternal ("User-Agent");
  837. request.Headers = headers;
  838. if (expect != null && expect.Length > 0)
  839. req.Expect = expect;
  840. if (accept != null && accept.Length > 0)
  841. req.Accept = accept;
  842. if (contentType != null && contentType.Length > 0)
  843. req.ContentType = contentType;
  844. if (connection != null && connection.Length > 0)
  845. req.Connection = connection;
  846. if (userAgent != null && userAgent.Length > 0)
  847. req.UserAgent = userAgent;
  848. if (referer != null && referer.Length > 0)
  849. req.Referer = referer;
  850. }
  851. responseHeaders = null;
  852. return request;
  853. }
  854. WebRequest SetupRequest (Uri uri, string method, bool is_upload)
  855. {
  856. WebRequest request = SetupRequest (uri);
  857. request.Method = DetermineMethod (uri, method, is_upload);
  858. return request;
  859. }
  860. byte [] ReadAll (Stream stream, int length, object userToken)
  861. {
  862. MemoryStream ms = null;
  863. bool nolength = (length == -1);
  864. int size = ((nolength) ? 8192 : length);
  865. if (nolength)
  866. ms = new MemoryStream ();
  867. // long total = 0;
  868. int nread = 0;
  869. int offset = 0;
  870. byte [] buffer = new byte [size];
  871. while ((nread = stream.Read (buffer, offset, size)) != 0) {
  872. if (nolength) {
  873. ms.Write (buffer, 0, nread);
  874. } else {
  875. offset += nread;
  876. size -= nread;
  877. }
  878. #if NET_2_0
  879. if (async){
  880. // total += nread;
  881. OnDownloadProgressChanged (new DownloadProgressChangedEventArgs (nread, length, userToken));
  882. }
  883. #endif
  884. }
  885. if (nolength)
  886. return ms.ToArray ();
  887. return buffer;
  888. }
  889. string UrlEncode (string str)
  890. {
  891. StringBuilder result = new StringBuilder ();
  892. int len = str.Length;
  893. for (int i = 0; i < len; i++) {
  894. char c = str [i];
  895. if (c == ' ')
  896. result.Append ('+');
  897. else if ((c < '0' && c != '-' && c != '.') ||
  898. (c < 'A' && c > '9') ||
  899. (c > 'Z' && c < 'a' && c != '_') ||
  900. (c > 'z')) {
  901. result.Append ('%');
  902. int idx = ((int) c) >> 4;
  903. result.Append ((char) hexBytes [idx]);
  904. idx = ((int) c) & 0x0F;
  905. result.Append ((char) hexBytes [idx]);
  906. } else {
  907. result.Append (c);
  908. }
  909. }
  910. return result.ToString ();
  911. }
  912. static void UrlEncodeAndWrite (Stream stream, byte [] bytes)
  913. {
  914. if (bytes == null)
  915. return;
  916. int len = bytes.Length;
  917. if (len == 0)
  918. return;
  919. for (int i = 0; i < len; i++) {
  920. char c = (char) bytes [i];
  921. if (c == ' ')
  922. stream.WriteByte ((byte) '+');
  923. else if ((c < '0' && c != '-' && c != '.') ||
  924. (c < 'A' && c > '9') ||
  925. (c > 'Z' && c < 'a' && c != '_') ||
  926. (c > 'z')) {
  927. stream.WriteByte ((byte) '%');
  928. int idx = ((int) c) >> 4;
  929. stream.WriteByte (hexBytes [idx]);
  930. idx = ((int) c) & 0x0F;
  931. stream.WriteByte (hexBytes [idx]);
  932. } else {
  933. stream.WriteByte ((byte) c);
  934. }
  935. }
  936. }
  937. #if NET_2_0
  938. public void CancelAsync ()
  939. {
  940. lock (this){
  941. if (async_thread == null)
  942. return;
  943. //
  944. // We first flag things as done, in case the Interrupt hangs
  945. // or the thread decides to hang in some other way inside the
  946. // event handlers, or if we are stuck somewhere else. This
  947. // ensures that the WebClient object is reusable immediately
  948. //
  949. Thread t = async_thread;
  950. CompleteAsync ();
  951. t.Interrupt ();
  952. }
  953. }
  954. void CompleteAsync ()
  955. {
  956. lock (this){
  957. is_busy = false;
  958. async_thread = null;
  959. }
  960. }
  961. // DownloadDataAsync
  962. public void DownloadDataAsync (Uri address)
  963. {
  964. DownloadDataAsync (address, null);
  965. }
  966. public void DownloadDataAsync (Uri address, object userToken)
  967. {
  968. if (address == null)
  969. throw new ArgumentNullException ("address");
  970. lock (this) {
  971. SetBusy ();
  972. async = true;
  973. async_thread = new Thread (delegate (object state) {
  974. object [] args = (object []) state;
  975. try {
  976. byte [] data = DownloadDataCore ((Uri) args [0], args [1]);
  977. OnDownloadDataCompleted (
  978. new DownloadDataCompletedEventArgs (data, null, false, args [1]));
  979. } catch (ThreadInterruptedException){
  980. OnDownloadDataCompleted (
  981. new DownloadDataCompletedEventArgs (null, null, true, args [1]));
  982. throw;
  983. } catch (Exception e){
  984. OnDownloadDataCompleted (
  985. new DownloadDataCompletedEventArgs (null, e, false, args [1]));
  986. }
  987. });
  988. object [] cb_args = new object [] {address, userToken};
  989. async_thread.Start (cb_args);
  990. }
  991. }
  992. // DownloadFileAsync
  993. public void DownloadFileAsync (Uri address, string fileName)
  994. {
  995. DownloadFileAsync (address, fileName, null);
  996. }
  997. public void DownloadFileAsync (Uri address, string fileName, object userToken)
  998. {
  999. if (address == null)
  1000. throw new ArgumentNullException ("address");
  1001. if (fileName == null)
  1002. throw new ArgumentNullException ("fileName");
  1003. lock (this) {
  1004. SetBusy ();
  1005. async = true;
  1006. async_thread = new Thread (delegate (object state) {
  1007. object [] args = (object []) state;
  1008. try {
  1009. DownloadFileCore ((Uri) args [0], (string) args [1], args [2]);
  1010. OnDownloadFileCompleted (
  1011. new AsyncCompletedEventArgs (null, false, args [2]));
  1012. } catch (ThreadInterruptedException){
  1013. OnDownloadFileCompleted (
  1014. new AsyncCompletedEventArgs (null, true, args [2]));
  1015. } catch (Exception e){
  1016. OnDownloadFileCompleted (
  1017. new AsyncCompletedEventArgs (e, false, args [2]));
  1018. }});
  1019. object [] cb_args = new object [] {address, fileName, userToken};
  1020. async_thread.Start (cb_args);
  1021. }
  1022. }
  1023. // DownloadStringAsync
  1024. public void DownloadStringAsync (Uri address)
  1025. {
  1026. DownloadStringAsync (address, null);
  1027. }
  1028. public void DownloadStringAsync (Uri address, object userToken)
  1029. {
  1030. if (address == null)
  1031. throw new ArgumentNullException ("address");
  1032. lock (this) {
  1033. SetBusy ();
  1034. async = true;
  1035. async_thread = new Thread (delegate (object state) {
  1036. object [] args = (object []) state;
  1037. try {
  1038. string data = encoding.GetString (DownloadDataCore ((Uri) args [0], args [1]));
  1039. OnDownloadStringCompleted (
  1040. new DownloadStringCompletedEventArgs (data, null, false, args [1]));
  1041. } catch (ThreadInterruptedException){
  1042. OnDownloadStringCompleted (
  1043. new DownloadStringCompletedEventArgs (null, null, true, args [1]));
  1044. } catch (Exception e){
  1045. OnDownloadStringCompleted (
  1046. new DownloadStringCompletedEventArgs (null, e, false, args [1]));
  1047. }});
  1048. object [] cb_args = new object [] {address, userToken};
  1049. async_thread.Start (cb_args);
  1050. }
  1051. }
  1052. // OpenReadAsync
  1053. public void OpenReadAsync (Uri address)
  1054. {
  1055. OpenReadAsync (address, null);
  1056. }
  1057. public void OpenReadAsync (Uri address, object userToken)
  1058. {
  1059. if (address == null)
  1060. throw new ArgumentNullException ("address");
  1061. lock (this) {
  1062. SetBusy ();
  1063. async = true;
  1064. async_thread = new Thread (delegate (object state) {
  1065. object [] args = (object []) state;
  1066. WebRequest request = null;
  1067. try {
  1068. request = SetupRequest ((Uri) args [0]);
  1069. WebResponse response = GetWebResponse (request);
  1070. Stream stream = response.GetResponseStream ();
  1071. OnOpenReadCompleted (
  1072. new OpenReadCompletedEventArgs (stream, null, false, args [1]));
  1073. } catch (ThreadInterruptedException){
  1074. if (request != null)
  1075. request.Abort ();
  1076. OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, null, true, args [1]));
  1077. } catch (Exception e){
  1078. OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, e, false, args [1]));
  1079. } });
  1080. object [] cb_args = new object [] {address, userToken};
  1081. async_thread.Start (cb_args);
  1082. }
  1083. }
  1084. // OpenWriteAsync
  1085. public void OpenWriteAsync (Uri address)
  1086. {
  1087. OpenWriteAsync (address, null);
  1088. }
  1089. public void OpenWriteAsync (Uri address, string method)
  1090. {
  1091. OpenWriteAsync (address, method, null);
  1092. }
  1093. public void OpenWriteAsync (Uri address, string method, object userToken)
  1094. {
  1095. if (address == null)
  1096. throw new ArgumentNullException ("address");
  1097. lock (this) {
  1098. SetBusy ();
  1099. async = true;
  1100. async_thread = new Thread (delegate (object state) {
  1101. object [] args = (object []) state;
  1102. WebRequest request = null;
  1103. try {
  1104. request = SetupRequest ((Uri) args [0], (string) args [1], true);
  1105. Stream stream = request.GetRequestStream ();
  1106. OnOpenWriteCompleted (
  1107. new OpenWriteCompletedEventArgs (stream, null, false, args [2]));
  1108. } catch (ThreadInterruptedException){
  1109. if (request != null)
  1110. request.Abort ();
  1111. OnOpenWriteCompleted (
  1112. new OpenWriteCompletedEventArgs (null, null, true, args [2]));
  1113. } catch (Exception e){
  1114. OnOpenWriteCompleted (
  1115. new OpenWriteCompletedEventArgs (null, e, false, args [2]));
  1116. }});
  1117. object [] cb_args = new object [] {address, method, userToken};
  1118. async_thread.Start (cb_args);
  1119. }
  1120. }
  1121. // UploadDataAsync
  1122. public void UploadDataAsync (Uri address, byte [] data)
  1123. {
  1124. UploadDataAsync (address, null, data);
  1125. }
  1126. public void UploadDataAsync (Uri address, string method, byte [] data)
  1127. {
  1128. UploadDataAsync (address, method, data, null);
  1129. }
  1130. public void UploadDataAsync (Uri address, string method, byte [] data, object userToken)
  1131. {
  1132. if (address == null)
  1133. throw new ArgumentNullException ("address");
  1134. if (data == null)
  1135. throw new ArgumentNullException ("data");
  1136. lock (this) {
  1137. SetBusy ();
  1138. async = true;
  1139. async_thread = new Thread (delegate (object state) {
  1140. object [] args = (object []) state;
  1141. byte [] data2;
  1142. try {
  1143. data2 = UploadDataCore ((Uri) args [0], (string) args [1], (byte []) args [2], args [3]);
  1144. OnUploadDataCompleted (
  1145. new UploadDataCompletedEventArgs (data2, null, false, args [3]));
  1146. } catch (ThreadInterruptedException){
  1147. OnUploadDataCompleted (
  1148. new UploadDataCompletedEventArgs (null, null, true, args [3]));
  1149. } catch (Exception e){
  1150. OnUploadDataCompleted (
  1151. new UploadDataCompletedEventArgs (null, e, false, args [3]));
  1152. }});
  1153. object [] cb_args = new object [] {address, method, data, userToken};
  1154. async_thread.Start (cb_args);
  1155. }
  1156. }
  1157. // UploadFileAsync
  1158. public void UploadFileAsync (Uri address, string fileName)
  1159. {
  1160. UploadFileAsync (address, null, fileName);
  1161. }
  1162. public void UploadFileAsync (Uri address, string method, string fileName)
  1163. {
  1164. UploadFileAsync (address, method, fileName, null);
  1165. }
  1166. public void UploadFileAsync (Uri address, string method, string fileName, object userToken)
  1167. {
  1168. if (address == null)
  1169. throw new ArgumentNullException ("address");
  1170. if (fileName == null)
  1171. throw new ArgumentNullException ("fileName");
  1172. lock (this) {
  1173. SetBusy ();
  1174. async = true;
  1175. async_thread = new Thread (delegate (object state) {
  1176. object [] args = (object []) state;
  1177. byte [] data;
  1178. try {
  1179. data = UploadFileCore ((Uri) args [0], (string) args [1], (string) args [2], args [3]);
  1180. OnUploadFileCompleted (
  1181. new UploadFileCompletedEventArgs (data, null, false, args [3]));
  1182. } catch (ThreadInterruptedException){
  1183. OnUploadFileCompleted (
  1184. new UploadFileCompletedEventArgs (null, null, true, args [3]));
  1185. } catch (Exception e){
  1186. OnUploadFileCompleted (
  1187. new UploadFileCompletedEventArgs (null, e, false, args [3]));
  1188. }});
  1189. object [] cb_args = new object [] {address, method, fileName, userToken};
  1190. async_thread.Start (cb_args);
  1191. }
  1192. }
  1193. // UploadStringAsync
  1194. public void UploadStringAsync (Uri address, string data)
  1195. {
  1196. UploadStringAsync (address, null, data);
  1197. }
  1198. public void UploadStringAsync (Uri address, string method, string data)
  1199. {
  1200. UploadStringAsync (address, method, data, null);
  1201. }
  1202. public void UploadStringAsync (Uri address, string method, string data, object userToken)
  1203. {
  1204. if (address == null)
  1205. throw new ArgumentNullException ("address");
  1206. if (data == null)
  1207. throw new ArgumentNullException ("data");
  1208. lock (this) {
  1209. SetBusy ();
  1210. async = true;
  1211. async_thread = new Thread (delegate (object state) {
  1212. object [] args = (object []) state;
  1213. try {
  1214. string data2 = UploadString ((Uri) args [0], (string) args [1], (string) args [2]);
  1215. OnUploadStringCompleted (
  1216. new UploadStringCompletedEventArgs (data2, null, false, args [3]));
  1217. } catch (ThreadInterruptedException){
  1218. OnUploadStringCompleted (
  1219. new UploadStringCompletedEventArgs (null, null, true, args [3]));
  1220. } catch (Exception e){
  1221. OnUploadStringCompleted (
  1222. new UploadStringCompletedEventArgs (null, e, false, args [3]));
  1223. }});
  1224. object [] cb_args = new object [] {address, method, data, userToken};
  1225. async_thread.Start (cb_args);
  1226. }
  1227. }
  1228. // UploadValuesAsync
  1229. public void UploadValuesAsync (Uri address, NameValueCollection values)
  1230. {
  1231. UploadValuesAsync (address, null, values);
  1232. }
  1233. public void UploadValuesAsync (Uri address, string method, NameValueCollection values)
  1234. {
  1235. UploadValuesAsync (address, method, values, null);
  1236. }
  1237. public void UploadValuesAsync (Uri address, string method, NameValueCollection values, object userToken)
  1238. {
  1239. if (address == null)
  1240. throw new ArgumentNullException ("address");
  1241. if (values == null)
  1242. throw new ArgumentNullException ("values");
  1243. lock (this) {
  1244. CheckBusy ();
  1245. async = true;
  1246. async_thread = new Thread (delegate (object state) {
  1247. object [] args = (object []) state;
  1248. try {
  1249. byte [] data = UploadValuesCore ((Uri) args [0], (string) args [1], (NameValueCollection) args [2], args [3]);
  1250. OnUploadValuesCompleted (
  1251. new UploadValuesCompletedEventArgs (data, null, false, args [3]));
  1252. } catch (ThreadInterruptedException){
  1253. OnUploadValuesCompleted (
  1254. new UploadValuesCompletedEventArgs (null, null, true, args [3]));
  1255. } catch (Exception e){
  1256. OnUploadValuesCompleted (
  1257. new UploadValuesCompletedEventArgs (null, e, false, args [3]));
  1258. }});
  1259. object [] cb_args = new object [] {address, method, values, userToken};
  1260. async_thread.Start (cb_args);
  1261. }
  1262. }
  1263. protected virtual void OnDownloadDataCompleted (DownloadDataCompletedEventArgs args)
  1264. {
  1265. CompleteAsync ();
  1266. if (DownloadDataCompleted != null)
  1267. DownloadDataCompleted (this, args);
  1268. }
  1269. protected virtual void OnDownloadFileCompleted (AsyncCompletedEventArgs args)
  1270. {
  1271. CompleteAsync ();
  1272. if (DownloadFileCompleted != null)
  1273. DownloadFileCompleted (this, args);
  1274. }
  1275. protected virtual void OnDownloadProgressChanged (DownloadProgressChangedEventArgs e)
  1276. {
  1277. if (DownloadProgressChanged != null)
  1278. DownloadProgressChanged (this, e);
  1279. }
  1280. protected virtual void OnDownloadStringCompleted (DownloadStringCompletedEventArgs args)
  1281. {
  1282. CompleteAsync ();
  1283. if (DownloadStringCompleted != null)
  1284. DownloadStringCompleted (this, args);
  1285. }
  1286. protected virtual void OnOpenReadCompleted (OpenReadCompletedEventArgs args)
  1287. {
  1288. CompleteAsync ();
  1289. if (OpenReadCompleted != null)
  1290. OpenReadCompleted (this, args);
  1291. }
  1292. protected virtual void OnOpenWriteCompleted (OpenWriteCompletedEventArgs args)
  1293. {
  1294. CompleteAsync ();
  1295. if (OpenWriteCompleted != null)
  1296. OpenWriteCompleted (this, args);
  1297. }
  1298. protected virtual void OnUploadDataCompleted (UploadDataCompletedEventArgs args)
  1299. {
  1300. CompleteAsync ();
  1301. if (UploadDataCompleted != null)
  1302. UploadDataCompleted (this, args);
  1303. }
  1304. protected virtual void OnUploadFileCompleted (UploadFileCompletedEventArgs args)
  1305. {
  1306. CompleteAsync ();
  1307. if (UploadFileCompleted != null)
  1308. UploadFileCompleted (this, args);
  1309. }
  1310. protected virtual void OnUploadProgressChanged (UploadProgressChangedEventArgs e)
  1311. {
  1312. if (UploadProgressChanged != null)
  1313. UploadProgressChanged (this, e);
  1314. }
  1315. protected virtual void OnUploadStringCompleted (UploadStringCompletedEventArgs args)
  1316. {
  1317. CompleteAsync ();
  1318. if (UploadStringCompleted != null)
  1319. UploadStringCompleted (this, args);
  1320. }
  1321. protected virtual void OnUploadValuesCompleted (UploadValuesCompletedEventArgs args)
  1322. {
  1323. CompleteAsync ();
  1324. if (UploadValuesCompleted != null)
  1325. UploadValuesCompleted (this, args);
  1326. }
  1327. protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  1328. {
  1329. WebResponse response = request.EndGetResponse (result);
  1330. responseHeaders = response.Headers;
  1331. return response;
  1332. }
  1333. #endif
  1334. #if NET_2_0
  1335. protected virtual
  1336. #endif
  1337. WebRequest GetWebRequest (Uri address)
  1338. {
  1339. return SetupRequest (address);
  1340. }
  1341. #if NET_2_0
  1342. protected virtual
  1343. #endif
  1344. WebResponse GetWebResponse (WebRequest request)
  1345. {
  1346. WebResponse response = request.GetResponse ();
  1347. responseHeaders = response.Headers;
  1348. return response;
  1349. }
  1350. }
  1351. }