Пример #1
0
        public static bool DownloadContent(string url, IDownloadComplete iDownCB, object objState)
        {
            if (cntEvent != ConnectEvent.Idel)
            {
                return false;
            }

            SetState(ConnectEvent.DownloadWaiting);
            icb = iDownCB;
            state = objState;

            try
            {
                 // Create a HttpWebrequest object to the desired URL.
              			myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
              	/**
                * If you are behind a firewall and you do not have your browser proxy setup
                * you need to use the following proxy creation code.

              	// Create a proxy object.
              	WebProxy myProxy = new WebProxy();

              	// Associate a new Uri object to the _wProxy object, using the proxy address
              	// selected by the user.
              	myProxy.Address = new Uri("http://myproxy");

              	// Finally, initialize the Web request object proxy property with the _wProxy
              	// object.
              	myHttpWebRequest.Proxy=myProxy;
                ***/

              	// Create an instance of the RequestState and assign the previous myHttpWebRequest
              	// object to its request field.
              	myRequestState = new RequestState();
              	myRequestState.request = myHttpWebRequest;

              	// Start the asynchronous request.
              	IAsyncResult result=
                    (IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);

              	// this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
              	//ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);

              	// The response came in the allowed time. The work processing will happen in the
              	// callback function.
                // event的方式会导致PSV死锁,可能是同步方式的问题,修改为忙等待方式
              	//allDone.WaitOne();

              	// Release the HttpWebResponse resource.
              	//myRequestState.response.Close();
            }
            catch(WebException e)
            {
                SetState(ConnectEvent.ErrorEnding);
            }
            catch(Exception e)
            {
                SetState(ConnectEvent.ErrorEnding);
            }

            return true;
        }
Пример #2
0
        public static bool DownloadContent(string url, IDownloadComplete iDownCB, object objState)
        {
            if (cntEvent != ConnectEvent.Idel)
            {
                return(false);
            }

            SetState(ConnectEvent.DownloadWaiting);
            icb   = iDownCB;
            state = objState;

            try
            {
                // Create a HttpWebrequest object to the desired URL.
                myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                /**
                 * If you are behind a firewall and you do not have your browser proxy setup
                 * you need to use the following proxy creation code.
                 *
                 * // Create a proxy object.
                 * WebProxy myProxy = new WebProxy();
                 *
                 * // Associate a new Uri object to the _wProxy object, using the proxy address
                 * // selected by the user.
                 * myProxy.Address = new Uri("http://myproxy");
                 *
                 *
                 * // Finally, initialize the Web request object proxy property with the _wProxy
                 * // object.
                 * myHttpWebRequest.Proxy=myProxy;
                 ***/

                // Create an instance of the RequestState and assign the previous myHttpWebRequest
                // object to its request field.
                myRequestState         = new RequestState();
                myRequestState.request = myHttpWebRequest;


                // Start the asynchronous request.
                IAsyncResult result =
                    (IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);

                // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
                //ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);

                // The response came in the allowed time. The work processing will happen in the
                // callback function.
                // event的方式会导致PSV死锁,可能是同步方式的问题,修改为忙等待方式
                //allDone.WaitOne();

                // Release the HttpWebResponse resource.
                //myRequestState.response.Close();
            }
            catch (WebException e)
            {
                SetState(ConnectEvent.ErrorEnding);
            }
            catch (Exception e)
            {
                SetState(ConnectEvent.ErrorEnding);
            }

            return(true);
        }