Exemplo n.º 1
0
        public object Request(string url, WebProxy proxy)
        {
            object result = null;

            try
            {
                if (aborted)
                {
                    throw new Exception();
                }

                //url = AppDomain.CurrentDomain.GetData("Url") as string;
                //proxy = AppDomain.CurrentDomain.GetData("Proxy") as WebProxy;
                rstop = new RequestState();

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                request.Proxy     = proxy;
                request.KeepAlive = false;

                Initialize(ref request);

                rstop.Request = request;
                //  Debug.WriteLine(url);
                Stopwatch st = new Stopwatch();
                st.Start();
                IAsyncResult r = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), rstop);
                ThreadPool.RegisterWaitForSingleObject(r.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), rstop.Request, DefaultTimeout, true);
                allDone.WaitOne();
                st.Stop();
                Debug.WriteLine(st.ElapsedMilliseconds);
                if (!_byte)
                {
                    if (rstop.RequestData != null)
                    {
                        if (!rstop.RequestData.ToString().Trim().Equals(string.Empty))
                        {
                            result = rstop.RequestData.ToString();
                        }
                    }
                }
                else
                {
                    if (rstop.RequestByte != null)
                    {
                        result = rstop.RequestByte;
                    }
                }
                if (rstop.ResponseStream != null)
                {
                    rstop.ResponseStream.Close();
                    Thread.Sleep(100);
                    //Debug.WriteLine("response stream closed "+url);
                }
            }
            catch (Exception)
            {
                throw new Exception();
            }

            return(result);
        }
Exemplo n.º 2
0
        void ReadCallBack(IAsyncResult asyncResult)
        {
            Stream       responseStream = null;
            IAsyncResult ar             = null;

            try
            {
                if (aborted)
                {
                    throw new Exception();
                }

                RequestState rs = (RequestState)asyncResult.AsyncState;
                responseStream = rs.ResponseStream;

                int read = responseStream.EndRead(asyncResult);

                if (read > 0)
                {
                    if (!_byte)
                    {
                        char[] charBuffer = new Char[BUFFER_SIZE];
                        int    len        = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
                        string str        = new String(charBuffer, 0, len);
                        rs.RequestData.Append(Encoding.ASCII.GetString(rs.BufferRead, 0, read));
                    }
                    else
                    {
                        rs.RequestByte.Write(rs.BufferRead, 0, read);
                    }

                    try
                    {
                        ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
                    }
                    catch (System.Net.Sockets.SocketException)
                    {
                        throw new Exception();
                    }
                    catch (System.Exception)
                    {
                        throw new Exception();
                    }
                }
                else
                {
                    responseStream.Close();
                    allDone.Set();
                }
            }
            catch (System.Exception)
            {
                if (responseStream != null)
                {
                    responseStream.Close();
                }

                allDone.Set();
            }
            return;
        }
Exemplo n.º 3
0
        public object Request(string url,WebProxy proxy)
        {
            object result = null;
            try
            {
                if (aborted)
                    throw new Exception();

                //url = AppDomain.CurrentDomain.GetData("Url") as string;
                //proxy = AppDomain.CurrentDomain.GetData("Proxy") as WebProxy;
                rstop = new RequestState();

               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                request.Proxy = proxy;
                request.KeepAlive = false;

                Initialize(ref request);

                rstop.Request = request;
              //  Debug.WriteLine(url);
                Stopwatch st = new Stopwatch();
                st.Start();
                IAsyncResult r = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), rstop);
                ThreadPool.RegisterWaitForSingleObject(r.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), rstop.Request, DefaultTimeout, true);
                allDone.WaitOne();
                st.Stop();
                Debug.WriteLine(st.ElapsedMilliseconds);
                if (!_byte)
                {
                    if (rstop.RequestData != null)
                    {
                        if (!rstop.RequestData.ToString().Trim().Equals(string.Empty))
                        {

                            result = rstop.RequestData.ToString();

                        }
                    }
                }
                else
                {
                    if (rstop.RequestByte != null)
                    {

                        result = rstop.RequestByte;
                    }

                }
                if (rstop.ResponseStream != null)
                {
                    rstop.ResponseStream.Close();
                    Thread.Sleep(100);
                    //Debug.WriteLine("response stream closed "+url);
                }

            }
            catch (Exception)
            {

                throw new Exception();
            }

            return result ;
        }