예제 #1
0
        public static string RetrieveBlob(HtmlBlobType htmlBlobType, string htmlBlobId, Uri uri, bool getIfNotExists = false)
        {
            // Get the blob if it doesn't exist
            if (getIfNotExists == true)
            {
                // This method will not do anything if the blob already exists
                HtmlBlob.GetAndStoreHtmlBlob(htmlBlobType, htmlBlobId, uri);
            }

            string         blobName       = HtmlBlob.ConstructBlobName(htmlBlobType, htmlBlobId, uri);
            CloudBlockBlob cloudBlockBlob = HtmlBlob.CloudBlobContainer.GetBlockBlobReference(blobName);

            string result = null;

            try
            {
                result = cloudBlockBlob.DownloadText();
            }
            catch (System.AggregateException e)
            {
                HtmlBlob.PrintGetHtmlPageException(cloudBlockBlob.SnapshotQualifiedUri, e);

                bool is404Exception = e.InnerExceptions.Where(ie =>
                                                              ie.GetType() == typeof(HttpRequestException) &&
                                                              ie.Message.IndexOf("404 (Not Found).", StringComparison.InvariantCultureIgnoreCase) >= 0).Any();

                if (!is404Exception)
                {
                    //throw;
                }
            }
            catch (Exception e)
            {
                HtmlBlob.PrintGetHtmlPageException(cloudBlockBlob.SnapshotQualifiedUri, e);
                //throw;
            }

            return(result);
        }
예제 #2
0
        public static string GetHtmlPage(Uri uri)
        {
            string responseString = null;

            try
            {
                HttpClient httpClient = new HttpClient();
                //httpClient.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                //httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36");
                Task <string> response = httpClient.GetStringAsync(uri);
                responseString = response.Result;
            }
            catch (System.AggregateException e)
            {
                HtmlBlob.PrintGetHtmlPageException(uri, e);

                bool is404Exception = e.InnerExceptions.Where(ie =>
                                                              ie.GetType() == typeof(HttpRequestException) &&
                                                              ie.Message.IndexOf("404 (Not Found).", StringComparison.InvariantCultureIgnoreCase) >= 0).Any();

                if (!is404Exception)
                {
                    //throw;
                }
                else
                {
                    responseString = "404";
                }
            }
            catch (Exception e)
            {
                HtmlBlob.PrintGetHtmlPageException(uri, e);
                //throw;
            }

            return(responseString);
        }
예제 #3
0
 private static void PrintGetHtmlPageException(Uri uri, Exception e)
 {
     HtmlBlob.PrintGetHtmlPageException(uri.AbsoluteUri, e);
 }