Beispiel #1
0
    public static Int64 GetUrlFileSizeEx(string url, string hostname, int timeout)
    {
        long size = -1;

        Easy easy = new Easy();

        Easy.SetCurrentEasy(easy);
        Slist headers = new Slist();

        headers.Append(HobaText.Format("Host: {0}", hostname));

        easy.SetOpt(CURLoption.CURLOPT_URL, url);
        easy.SetOpt(CURLoption.CURLOPT_HEADER, 1L);
        easy.SetOpt(CURLoption.CURLOPT_NOBODY, 1L);
        easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers);
        //easy.SetOpt(CURLoption.CURLOPT_IPRESOLVE, (long)CURLipResolve.CURL_IPRESOLVE_V4);
        easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000);

        easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L);
        easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L);

        int    error = 0;
        double downloadFileLength = 0.0f;

        if (easy.Perform() == CURLcode.CURLE_OK)
        {
            CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error);

            if (code == CURLcode.CURLE_OK && error == 200)
            {
                easy.GetInfo(CURLINFO.CURLINFO_CONTENT_LENGTH_DOWNLOAD, ref downloadFileLength);
            }

            if (downloadFileLength >= 0.0f)
            {
                size = (int)downloadFileLength;
            }
        }
        else
        {
            size = -1;
        }

        headers.FreeAll();
        Easy.SetCurrentEasy(null);
        easy.Cleanup();

        return(size);
    }
Beispiel #2
0
    public static string GetUrlContentType(string url, string hostname, int timeout)
    {
        Easy easy = new Easy();

        Easy.SetCurrentEasy(easy);
        Slist headers = new Slist();

        headers.Append(HobaText.Format("Host: {0}", hostname));

        easy.SetOpt(CURLoption.CURLOPT_URL, url);
        easy.SetOpt(CURLoption.CURLOPT_HEADER, 1L);
        easy.SetOpt(CURLoption.CURLOPT_NOBODY, 1L);
        easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers);
        //easy.SetOpt(CURLoption.CURLOPT_IPRESOLVE, (long)CURLipResolve.CURL_IPRESOLVE_V4);
        easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000);

        easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L);
        easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L);

        int    error       = 0;
        string contentType = "";

        if (easy.Perform() == CURLcode.CURLE_OK)
        {
            CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error);

            if (code == CURLcode.CURLE_OK && error == 200)
            {
                easy.GetInfo(CURLINFO.CURLINFO_CONTENT_TYPE, ref contentType);
            }
        }

        headers.FreeAll();
        Easy.SetCurrentEasy(null);
        easy.Cleanup();

        return(contentType);
    }
Beispiel #3
0
        public bool DoDownload(string url, string hostname, string destFileName, int timeout, long bufferSize, WRITE_DATA_DELEGATE onWriteData, Object extraData)
        {
            bool ret  = false;
            Easy easy = new Easy();

            Easy.SetCurrentEasy(easy);

            Slist headers = new Slist();

            Easy.WriteFunction wf       = new Easy.WriteFunction(onWriteData);
            CURLcode           curlCode = CURLcode.CURLE_OK;

            try
            {
                headers.Append(HobaText.Format("Host: {0}", hostname));
                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, extraData);
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
                easy.SetOpt(CURLoption.CURLOPT_HEADER, 0L);
                easy.SetOpt(CURLoption.CURLOPT_VERBOSE, 0L);
                easy.SetOpt(CURLoption.CURLOPT_BUFFERSIZE, (long)bufferSize);
                easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers);
                //easy.SetOpt(CURLoption.CURLOPT_IPRESOLVE, (long)CURLipResolve.CURL_IPRESOLVE_V4);
                easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000);

                //
                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L);
                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L);

                if (StartPoint > 0)
                {
                    easy.SetOpt(CURLoption.CURLOPT_RESUME_FROM, StartPoint);
                }

                int error = 0;
                curlCode = easy.Perform();
                if (curlCode == CURLcode.CURLE_OK)
                {
                    CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error);
                    if (code == CURLcode.CURLE_OK && error == 200)
                    {
                        ret = true;
                    }
                }
            }
            finally
            {
                headers.FreeAll();

                Easy.SetCurrentEasy(null);
                easy.Cleanup();
            }

            if (curlCode != CURLcode.CURLE_OK)
            {
                throw new WebException(String.Format(
                                           "Error downloading \"{0}\": {1}", url, curlCode));
            }

            return(ret);
        }
Beispiel #4
0
        public long GetFileSize(string url, string hostName, int timeout, ref string modifiedTimeGMT)
        {
            long size = -1;

            modifiedTimeGMT = "";

            Easy easy = new Easy();

            Easy.SetCurrentEasy(easy);

            Slist    headers  = new Slist();
            CURLcode curlCode = CURLcode.CURLE_OK;

            try
            {
                headers.Append(HobaText.Format("Host: {0}", hostName));
                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                easy.SetOpt(CURLoption.CURLOPT_FILETIME, 1L);
                easy.SetOpt(CURLoption.CURLOPT_HEADER, 1L);
                easy.SetOpt(CURLoption.CURLOPT_NOBODY, 1L);
                easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers);
                easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000);

                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L);
                easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L);

                int    error = 0;
                double downloadFileLength = -1.0f;
                curlCode = easy.Perform();
                if (curlCode == CURLcode.CURLE_OK)
                {
                    CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error);

                    if (code == CURLcode.CURLE_OK && error == 200)
                    {
                        easy.GetInfo(CURLINFO.CURLINFO_CONTENT_LENGTH_DOWNLOAD, ref downloadFileLength);

                        DateTime time = new DateTime();
                        code = easy.GetInfo(CURLINFO.CURLINFO_FILETIME, ref time);
                        if (code == CURLcode.CURLE_OK)
                        {
                            modifiedTimeGMT = time.ToUniversalTime().ToString("R");
                        }
                    }

                    if (downloadFileLength >= 0.0f)
                    {
                        size = (int)downloadFileLength;
                    }
                }
                else
                {
                    size = -1;
                }
            }
            finally
            {
                headers.FreeAll();
                Easy.SetCurrentEasy(null);
                easy.Cleanup();
            }

            if (curlCode != CURLcode.CURLE_OK)
            {
                throw new ArgumentException(String.Format(
                                                "Error downloading \"{0}\": {1}", url, curlCode));
            }

            return(size);
        }