Exemplo n.º 1
0
        public void DownloadAsync(string url, Action<WebDownloadResult> resultAction)
        {
            WebDownloadResult response = new WebDownloadResult();
            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                IAsyncResult result = (IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult tempResult)
                {
                    HttpWebResponse webResponse = (HttpWebResponse)myHttpWebRequest.EndGetResponse(tempResult);
                    Stream responseStream = webResponse.GetResponseStream();

                    using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        response.ResultString = reader.ReadToEnd();
                        response.StatusCode = webResponse.StatusCode;
                        response.StatusCodeNumber = (int)webResponse.StatusCode;

                        if (resultAction != null) resultAction(response);
                    }
                }), null);


            }
            catch(Exception ex)
            {
                response.ErrorOccured = true;
                if (resultAction != null) resultAction(response);
            }
        }
Exemplo n.º 2
0
    public static void Download(string url, Action <WebDownloadResult> resultAction)
    {
        WebDownloadResult response = new WebDownloadResult();

        try
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            IAsyncResult   result           = (IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult tempResult)
            {
                HttpWebResponse webResponse = (HttpWebResponse)myHttpWebRequest.EndGetResponse(tempResult);
                Stream responseStream       = webResponse.GetResponseStream();
                using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
                {
                    response.ResultString     = reader.ReadToEnd();
                    response.StatusCode       = webResponse.StatusCode;
                    response.StatusCodeNumber = (int)webResponse.StatusCode;

                    if (resultAction != null)
                    {
                        resultAction(response);
                    }
                }
            }), null);
        }
        catch
        {
            response.ErrorOccured = true;
            if (resultAction != null)
            {
                resultAction(response);
            }
        }
    }