Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / C#

WWW DSL using Irony - Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
1 Jun 2009CPOL4 min read 31.6K   236   30  
A Domain Specific Language for WWW operations created with Irony.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Irony.Runtime;
using System.Threading;

namespace WWWDSL
{
    static class DownloadWebPage
    {
        static private void WaitWithAbortCheck(WebRequest request, IAsyncResult aRes, EvaluationContext evalContext)
        {
            while (!aRes.IsCompleted)
            {
                Thread.Sleep(100);
                try
                {
                    evalContext.GetProcessingContext().AbortedCheck();
                }
                catch
                {
                    request.Abort();
                    throw;
                }
            }
        }

        static public string DownloadWWWPage(string url, string method, string referer, string postData, 
                                             EvaluationContext evalContext)
        {
            HttpWebRequest webRequestObject = (HttpWebRequest)HttpWebRequest.Create(url);

            webRequestObject.UserAgent = "WWW DSL 1.0";
            webRequestObject.Method = method;
            if (referer != null)
            {
                webRequestObject.Referer = referer;
            }

            if (postData != null)
            {
                ((WwwDslEvaluationContext)evalContext).OnProgressChange("Sending request", -1);
                
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(postData);
                webRequestObject.ContentType = "application/x-www-form-urlencoded";
                //webRequestObject.ContentLength = data.Length;
                IAsyncResult aRes = webRequestObject.BeginGetRequestStream(null, null);
                WaitWithAbortCheck(webRequestObject, aRes, evalContext);
                using (Stream newStream = webRequestObject.EndGetRequestStream(aRes))// BeginGetRequestStream( GetRequestStream();
                {
                    // Send the data.
                    aRes = newStream.BeginWrite(data, 0, data.Length, null, null);
                    WaitWithAbortCheck(webRequestObject, aRes, evalContext);
                    newStream.EndWrite(aRes);
                }
                
                ((WwwDslEvaluationContext)evalContext).OnProgressChange("Request sent", -1);
            }

            ((WwwDslEvaluationContext)evalContext).OnProgressChange("Getting response", -1);
            string result;
            IAsyncResult aResult = webRequestObject.BeginGetResponse(null, null);
            WaitWithAbortCheck(webRequestObject, aResult, evalContext);
            using (WebResponse response = webRequestObject.EndGetResponse(aResult))
            {
                ((WwwDslEvaluationContext)evalContext).OnLogAction(
                    string.Format("URL:{0}, download from:{1}", url, response.ResponseUri.ToString()));
                using (Stream webStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(webStream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            ((WwwDslEvaluationContext)evalContext).OnProgressChange("Response obtained", -1);

            return result;
        }

        static public void DownloadFile(string url, string path, EvaluationContext evalContext)
        {
            HttpWebRequest webRequestObject = (HttpWebRequest)HttpWebRequest.Create(url);

            webRequestObject.UserAgent = "WWW DSL 1.0";

            ((WwwDslEvaluationContext)evalContext).OnProgressChange("Starting download", -1);

            //WebResponse Response = webRequestObject.GetResponse();
            IAsyncResult aResult = webRequestObject.BeginGetResponse(null, null);
            WaitWithAbortCheck(webRequestObject, aResult, evalContext);
            using (WebResponse response = webRequestObject.EndGetResponse(aResult))
            {
                ((WwwDslEvaluationContext)evalContext).OnLogAction(
                    string.Format("URL:{0}, download from:{1}",url, response.ResponseUri.ToString()));
                using (Stream webStream = response.GetResponseStream())
                {
                    long filesize = response.ContentLength;
                    string fullPath = Path.Combine(path, Uri.UnescapeDataString(response.ResponseUri.Segments.Last()));
                    using (FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
                    {
                        const int size = 4096;
                        byte[] bytes = new byte[size];
                        int numBytes;
                        long downloadedsize = 0;
                        DateTime start = DateTime.Now;

                        while ((numBytes = webStream.Read(bytes, 0, size)) > 0)
                        {
                            evalContext.GetProcessingContext().AbortedCheck();
                            fs.Write(bytes, 0, numBytes);
                            downloadedsize += numBytes;
                            if ((DateTime.Now - start).TotalSeconds > 0)
                            {
                                ((WwwDslEvaluationContext)evalContext).OnProgressChange(
                                    String.Format("Downloaded {0} kB ({1:F3} Mb/s)",
                                    downloadedsize / 1024,
                                    8 * downloadedsize / (DateTime.Now - start).TotalSeconds / 1e6),
                                    (int)(100 * downloadedsize / (double)filesize),
                                    false);
                            }
                            else
                            {
                                ((WwwDslEvaluationContext)evalContext).OnProgressChange(
                                    String.Format("Downloaded {0} kB", downloadedsize / 1024),
                                    (int)(100 * downloadedsize / (double)filesize),
                                    false);
                            }
                        }
                    }
                }
            }

            ((WwwDslEvaluationContext)evalContext).OnProgressChange("Download finished", -1);
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions