Click here to Skip to main content
15,894,405 members
Articles / Desktop Programming / WPF

Reflection Studio - Part 1 - Introduction: Architecture and Design

Rate me:
Please Sign up or sign in to vote.
4.83/5 (23 votes)
22 Sep 2010GPL36 min read 60.1K   6.9K   111  
Reflection Studio is a "developer" application for assembly, database, performance, and code generation, written in C# under WPF 4.0.
using System;
using System.IO;
using System.Net;
using System.Text;
using ReflectionStudio.Core.Events;

namespace ReflectionStudio.Core.Helpers
{
	/// <summary>
	/// UrlSaveHelper allow to save the response of any HTTP GET in a synchrone way. For async, use AsyncUrlSaveHelper
	/// </summary>
	public class UrlSaveHelper
	{
		private string _webUrl;
		private string _destinationFile;

		/// <summary>
		/// Constructor, init the context
		/// </summary>
		/// <param name="url"></param>
		/// <param name="destinationFile"></param>
		public UrlSaveHelper(string url, string destinationFile)
		{
			_webUrl = url;
			_destinationFile = destinationFile;
		}

		/// <summary>
		/// Save the response of any HTTP GET in a synchrone way
		/// </summary>
		/// <returns></returns>
		public bool SaveUrlContent()
		{
			try
			{
				Tracer.Verbose("UrlSaveHelper.SaveUrlContent", "Try to save resource {0} into {1}", _webUrl, _destinationFile);

				// used to build entire input
				StringBuilder sb = new StringBuilder();

				// used on each read operation
				byte[] buf = new byte[8192];

				// prepare the web page we will be asking for
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_webUrl);

				// execute the request
				HttpWebResponse response = (HttpWebResponse)request.GetResponse();

				// we will read data via the response stream
				Stream resStream = response.GetResponseStream();

				string tempString = null;
				int count = 0;

				do
				{
					// fill the buffer with data
					count = resStream.Read(buf, 0, buf.Length);

					// make sure we read some data
					if (count != 0)
					{
						// translate from bytes to ASCII text
						tempString = Encoding.ASCII.GetString(buf, 0, count);

						// continue building the string
						sb.Append(tempString);
					}
				}
				while (count > 0); // any more data to read?

				//sb.Remove(0, 3);
				File.WriteAllText(_destinationFile, sb.ToString());
			}
			catch (Exception all)
			{
				Tracer.Error("UrlSaveHelper.SaveUrlContent", all);
				Tracer.Verbose("UrlSaveHelper.SaveUrlContent", "Error retreiving resource {0} to save it into {1}", _webUrl, _destinationFile);
				return false;
			}

			Tracer.Verbose("UrlSaveHelper.SaveUrlContent", "Resource {0} saved it into {1}", _webUrl, _destinationFile);
			return true;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions