Пример #1
0
        protected bool CheckURLFileExists(string FileURL,
                                          ref string actualURL, out DateTime lastModified)
        {
            lastModified = DateTime.MinValue;
            if (System.IO.File.Exists(FileURL))
            {
                return(false);
            }
            long tmp = 0;

            if (RemoteFileInformation.TryGetFileInformation(FileURL, ref actualURL, ref lastModified, ref tmp))
            {
                return(true);
            }
            bool retVal = false;



            // to make sure that this call is single threaded and tom update the remoteFilesDt list.
            lock (RemoteFiles.remoteFilesDt)
            {
                int count   = 0;
                int timeout = 5000;
                while (true)
                {
                    HttpWebResponse webResponse = null;
                    try
                    {
                        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(FileURL);
                        myRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(this.CachePolicy);
                        myRequest.Timeout     = timeout;                        // waiting 5 seconds
                        myRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        myRequest.KeepAlive   = false;
                        //used fiddler to determine what IE was setting the UserAgent to and used the same to
                        //make this call work....
                        myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.5.30428)";
                        IWebProxy iwp20    = WebRequest.DefaultWebProxy;
                        Uri       proxyUri = iwp20.GetProxy(myRequest.RequestUri);
                        WebProxy  theProxy = new WebProxy(proxyUri);
                        if (!string.Equals(theProxy.Address.AbsoluteUri, myRequest.RequestUri.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase))
                        {
                            theProxy.UseDefaultCredentials = true;
                            myRequest.Proxy = theProxy;
                        }
                        //myRequest.IfModifiedSince //304
                        //				myRequest.Proxy = new WebProxy(FileURL, true);
                        //				myRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        webResponse = (HttpWebResponse)myRequest.GetResponse();
                        switch (webResponse.StatusCode)
                        {
                        case (HttpStatusCode.Accepted):
                        case (HttpStatusCode.Found):
                        case (HttpStatusCode.OK):
                            //make sure that the file returned is the file requested
                            retVal = true;
                            if (string.Compare(FileURL, webResponse.ResponseUri.AbsoluteUri, true) != 0)
                            {
                                //uri did not match
                                //just check file name
                                if (string.Compare(System.IO.Path.GetFileName(FileURL), System.IO.Path.GetFileName(webResponse.ResponseUri.AbsoluteUri), true) != 0)
                                {
                                    //we got some website redirect that is not the file we asked for and so it is not a valid uri....
                                    retVal = false;
                                }
                            }

                            // many invalid urls are listed as for sale on other sites like goDaddy.com
                            // This means even though the url is not valid, we will get a response of OK
                            // and content type other than text/xml
                            // However some taxonomy schemas are posted as text/plain. So if our response is
                            // of type plain/text we need to parse the content to see if it is a valid xml file
                            if (!webResponse.ContentType.ToLower().Contains("/xml"))
                            {
                                //Get the contenst of the file at the given url so that we can
                                //determin if it represents xml data
                                WebClient client = new WebClient();
                                if (!string.Equals(theProxy.Address, myRequest.RequestUri.AbsoluteUri))
                                {
                                    theProxy.UseDefaultCredentials = true;
                                    client.Proxy = theProxy;
                                }
                                string content = client.DownloadString(FileURL);

                                //Test for urls that return empty text data
                                if (content.Length == 0)
                                {
                                    retVal = false;
                                }
                                else
                                {
                                    try
                                    {
                                        XmlDocument doc = new XmlDocument();
                                        doc.LoadXml(content);
                                    }
                                    catch (Exception)
                                    {
                                        retVal = false;
                                    }
                                }
                            }

                            lastModified = webResponse.LastModified;
                            actualURL    = webResponse.ResponseUri.AbsoluteUri;
                            break;

                        case (HttpStatusCode.NotFound):
                            retVal = false;
                            break;

                        default:
                            retVal = false;
                            break;
                        }
                    }
                    //			catch(WebException we)
                    //			{
                    //				if ( we.InnerException != null )
                    //				{
                    //					System.Windows.Forms.MessageBox.Show( "Error caught in CheckURLFileExists: " + we.Message + " Inner: " + we.InnerException.Message );
                    //				}
                    //				else
                    //				{
                    //					System.Windows.Forms.MessageBox.Show( "Error caught in CheckURLFileExists: " + we.Message );
                    //				}
                    //
                    //				retVal = false;
                    //			}
                    catch (WebException exp)
                    {
                        if (exp.Status == WebExceptionStatus.ConnectionClosed || exp.Status == WebExceptionStatus.Timeout)
                        {
                            if (exp.Status == WebExceptionStatus.Timeout)
                            {
                                //give the application couple of more sec to get the data.
#if DEBUG
                                Console.WriteLine("Time out occured for URL " + FileURL);
#endif
                                timeout += 2000;
                            }
                            //it is a good idea to retry this before we give up..
                            count++;
                            if (count <= 5)
                            {
                                continue;
                            }
                        }
                        retVal = false;
                    }
                    catch (System.InvalidCastException)
                    {
                        retVal = false;
                    }
                    catch (System.UriFormatException)
                    {
                        retVal = false;
                    }
                    catch (Exception Ex)
                    {
                        throw (new AucentException("Engine.Error.OpenFileFailure", Ex.Message, Ex));
                    }
                    finally
                    {
                        if (webResponse != null)
                        {
                            webResponse.Close();

                            //(webResponse as IDisposable).Dispose();
                        }
                    }
                    if (retVal)
                    {
                        RemoteFiles.remoteFilesDt[FileURL] = actualURL;
                        //RemoteFileInformation.AddFileInformation(FileURL, actualURL, lastModified);
                    }

                    return(retVal);
                }
            }
        }
Пример #2
0
        public bool LoadWebFile(string FileURL,
                                ref string actualURL,
                                out XmlDocument dataFile)
        {
            dataFile = null;
            if (!FileURL.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            string isolatedStorageFileName = AucentGeneral.RivetApplicationDataDragonTagPath + Path.DirectorySeparatorChar + Path.GetFileName(FileURL);

            actualURL = FileURL;
            DateTime lastModified           = DateTime.Now;
            bool     useIsolatedStorageFile = false;
            FileInfo fi         = new FileInfo(isolatedStorageFileName);
            long     fileLength = 0;

            if (RemoteFileInformation.TryGetFileInformation(FileURL, ref actualURL, ref lastModified, ref fileLength))
            {
                if (fi.Exists && fi.LastWriteTime >= lastModified && fi.Length == fileLength)
                {
                    dataFile = new XmlDocument();
                    dataFile.Load(isolatedStorageFileName);

                    return(true);
                }
                else if (fi.Exists)
                {
                    //wrong date or length
                    try
                    {
                        fi.Delete();
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        fi = new FileInfo(isolatedStorageFileName);
                    }
                }
            }



            if (fi.Exists)
            {
                HttpWebResponse webResponse = null;
                try
                {
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(FileURL);
                    myRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(this.CachePolicy);
                    myRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    myRequest.KeepAlive   = false;
                    myRequest.Timeout     = 1000;
                    //used fiddler to determine what IE was setting the UserAgent to and used the same to
                    //make this call work....
                    myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.5.30428)";
                    myRequest.Method    = "Head";

                    IWebProxy iwp20    = WebRequest.DefaultWebProxy;
                    Uri       proxyUri = iwp20.GetProxy(myRequest.RequestUri);
                    WebProxy  theProxy = new WebProxy(proxyUri);
                    if (!string.Equals(theProxy.Address.AbsoluteUri, myRequest.RequestUri.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase))
                    {
                        theProxy.UseDefaultCredentials = true;
                        myRequest.Proxy = theProxy;
                    }

                    webResponse = (HttpWebResponse)myRequest.GetResponse();
                    switch (webResponse.StatusCode)
                    {
                    case (HttpStatusCode.Accepted):
                    case (HttpStatusCode.Found):
                    case (HttpStatusCode.OK):
                        if (fi.LastWriteTime >= webResponse.LastModified)
                        {
                            lastModified = webResponse.LastModified;
                            actualURL    = webResponse.ResponseUri.AbsoluteUri;
                            dataFile     = new XmlDocument();
                            dataFile.Load(isolatedStorageFileName);
                            RemoteFiles.remoteFilesDt[FileURL] = actualURL;
                            RemoteFileInformation.AddFileInformation(FileURL, actualURL, lastModified, fi.Length);

                            return(true);
                        }
                        break;
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (webResponse != null)
                    {
                        webResponse.Close();

                        (webResponse as IDisposable).Dispose();
                    }
                }
            }



            bool retVal = false;


            // to make sure that this call is single threaded and tom update the remoteFilesDt list.
            lock (RemoteFiles.remoteFilesDt)
            {
                int count   = 0;
                int timeout = 5000;
                while (true)
                {
                    HttpWebResponse webResponse = null;
                    try
                    {
                        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(FileURL);
                        myRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(this.CachePolicy);
                        myRequest.Timeout     = timeout;                        // waiting 5 seconds
                        myRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        myRequest.KeepAlive   = false;
                        //used fiddler to determine what IE was setting the UserAgent to and used the same to
                        //make this call work....
                        myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.5.30428)";
                        IWebProxy iwp20    = WebRequest.DefaultWebProxy;
                        Uri       proxyUri = iwp20.GetProxy(myRequest.RequestUri);
                        WebProxy  theProxy = new WebProxy(proxyUri);
                        if (!string.Equals(theProxy.Address.AbsoluteUri, myRequest.RequestUri.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase))
                        {
                            theProxy.UseDefaultCredentials = true;
                            myRequest.Proxy = theProxy;
                        }

                        //myRequest.IfModifiedSince //304
                        //				myRequest.Proxy = new WebProxy(FileURL, true);
                        //				myRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        webResponse = (HttpWebResponse)myRequest.GetResponse();

                        switch (webResponse.StatusCode)
                        {
                        case (HttpStatusCode.Accepted):
                        case (HttpStatusCode.Found):
                        case (HttpStatusCode.OK):
                            //make sure that the file returned is the file requested
                            retVal       = true;
                            lastModified = webResponse.LastModified;
                            actualURL    = webResponse.ResponseUri.AbsoluteUri;


                            useIsolatedStorageFile = false;


                            if (fi.Exists && fi.LastWriteTime >= lastModified)
                            {
                                useIsolatedStorageFile = true;
                            }


                            if (useIsolatedStorageFile)
                            {
                                FileInfo tmp = new FileInfo(isolatedStorageFileName);
                                RemoteFileInformation.AddFileInformation(FileURL, actualURL, lastModified, tmp.Length);


                                dataFile = new XmlDocument();
                                dataFile.Load(isolatedStorageFileName);
                                return(true);
                            }
                            else
                            {
                                try
                                {
                                    dataFile = new XmlDocument();
                                    dataFile.Load(webResponse.GetResponseStream());
                                }
                                catch (Exception)
                                {
                                    return(false);
                                }

                                RemoteFiles.remoteFilesDt[FileURL] = actualURL;
                                if (retVal && isolatedStorageFileName != null)
                                {
                                    dataFile.Save(isolatedStorageFileName);

                                    FileInfo tmp = new FileInfo(isolatedStorageFileName);
                                    RemoteFileInformation.AddFileInformation(FileURL, actualURL, lastModified, tmp.Length);
                                }


                                return(true);
                            }



                        case (HttpStatusCode.NotFound):
                            retVal = false;
                            break;

                        default:
                            retVal = false;
                            break;
                        }
                    }
                    catch (WebException exp)
                    {
                        if (exp.Status == WebExceptionStatus.ConnectionClosed || exp.Status == WebExceptionStatus.Timeout)
                        {
                            if (exp.Status == WebExceptionStatus.Timeout)
                            {
                                //give the application couple of more sec to get the data.

                                timeout += 2000;
                            }
                            //it is a good idea to retry this before we give up..
                            count++;
                            if (count <= 5)
                            {
                                continue;
                            }
                        }
                        retVal = false;
                    }
                    catch (System.InvalidCastException)
                    {
                        return(false);
                    }
                    catch (System.UriFormatException)
                    {
                        return(false);
                    }
                    catch (Exception)
                    {
                        return(false);
                    }
                    finally
                    {
                        if (webResponse != null)
                        {
                            webResponse.Close();

                            (webResponse as IDisposable).Dispose();
                        }
                    }


                    return(retVal);
                }
            }
        }