示例#1
0
        private static string[] GetPageAuth(String url, bool useCache, ICredentials credentials)
        {
            WebResponse result = null;

            StringBuilder stringBuilder = new StringBuilder("", 4000);

            try
            {
                if (useCache && SimpleWebCache.Available(url))
                {
                    return(SimpleWebCache.Read(url));
                }

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.UserAgent = UserAgent();

                req.Credentials = credentials;

                result = req.GetResponse();
                Stream       ReceiveStream = result.GetResponseStream();
                Encoding     encode        = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader sr            = new StreamReader(ReceiveStream, encode);
                // Console.WriteLine("\r\nResponse stream received");
                if (true)
                {
                    Char[] read      = new Char[256];
                    int    count     = sr.Read(read, 0, 256);
                    int    bytesRead = count * 16;
                    while (count > 0)
                    {
                        String str = new String(read, 0, count);
                        stringBuilder.Append(str);
                        count      = sr.Read(read, 0, 256);
                        bytesRead += count * 16;
                        if (OnProgress != null)
                        {
                            OnProgress(null, new BytesReadEventArgs("reading", bytesRead));
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                Console.WriteLine("\r\nThe request URI could not be found or was malformed " + ex.Message);
                throw ex;
            }
            finally
            {
                if (result != null)
                {
                    result.Close();
                }
            }

            stringBuilder.Replace("\r", "");
            string[] rval = stringBuilder.ToString().Split(new char[] { '\n' });

            if (useCache)
            {
                SimpleWebCache.Save(url, rval);
            }

            return(rval);
        }
示例#2
0
        /// <summary>
        /// Gets page using GET method.
        /// </summary>
        public static string[] GetPage(String url, bool useCache, string username, string password)
        {
            WebResponse result = null;

            Logger.WriteLine(url);
            StringBuilder stringBuilder = new StringBuilder("", 4000);

            try
            {
                if (useCache && SimpleWebCache.Available(url))
                {
                    return(SimpleWebCache.Read(url));
                }

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                SetupProxy(req);
                req.UserAgent = UserAgent();
                if (username != "" && password != "")
                {
                    NetworkCredential nc = new NetworkCredential(username, password);
                    CredentialCache   c  = new CredentialCache();
                    c.Add(new Uri(url), "NTLM", nc);
                    req.Credentials = c;
                }


                result = req.GetResponse();
                Stream       ReceiveStream = result.GetResponseStream();
                Encoding     encode        = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader sr            = new StreamReader(ReceiveStream, encode);
                // Console.WriteLine("\r\nResponse stream received");
                if (true)
                {
                    Char[] read      = new Char[256];
                    int    count     = sr.Read(read, 0, 256);
                    int    bytesRead = count * 16;
                    while (count > 0)
                    {
                        String str = new String(read, 0, count);
                        stringBuilder.Append(str);
                        count      = sr.Read(read, 0, 256);
                        bytesRead += count * 16;
                        if (OnProgress != null)
                        {
                            OnProgress(null, new BytesReadEventArgs("reading", bytesRead));
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                Console.WriteLine("\r\nDebug0:The request URI could not be found or was malformed " + ex.Message);
                if (result != null)
                {
                    Console.WriteLine("Debug1:");
                    Console.WriteLine("Debug2: " + result.ToString());
                }
                throw ex;
            }
            finally
            {
                if (result != null)
                {
                    result.Close();
                }
            }

            stringBuilder.Replace("\r", "");
            string[] rval = stringBuilder.ToString().Split(new char[] { '\n' });

            if (useCache)
            {
                SimpleWebCache.Save(url, rval);
            }

            return(rval);
        }
示例#3
0
        public static string[] GetPage(String url, String payload, bool useCache)
        {
            Logger.WriteLine(url);
            Logger.WriteLine(payload);
            if (useCache && SimpleWebCache.Available(url + payload))
            {
                return(SimpleWebCache.Read(url + payload));
            }

            StringBuilder stringBuilder = new StringBuilder("", 4000);
            WebResponse   result        = null;

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method    = "POST";
                req.UserAgent = UserAgent();

                SetupProxy(req);

                //  req.Headers.Add("user-agent",UserAgent());
                req.ContentType = "application/x-www-form-urlencoded";
                StringBuilder UrlEncoded = new StringBuilder();
                Char[]        reserved   = { '?', '=', '&' };
                byte[]        SomeBytes  = null;

                if (payload != null)
                {
                    int i = 0, j;
                    while (i < payload.Length)
                    {
                        j = payload.IndexOfAny(reserved, i);
                        if (j == -1)
                        {
                            UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length - i)));
                            break;
                        }
                        UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j - i)));
                        UrlEncoded.Append(payload.Substring(j, 1));
                        i = j + 1;
                    }
                    SomeBytes         = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                    req.ContentLength = SomeBytes.Length;
                    Stream newStream = req.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    newStream.Close();
                }
                else
                {
                    req.ContentLength = 0;
                }


                result = req.GetResponse();
                Stream       ReceiveStream = result.GetResponseStream();
                Encoding     encode        = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader sr            = new StreamReader(ReceiveStream, encode);
                Char[]       read          = new Char[256];
                int          count         = sr.Read(read, 0, 256);

                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    //Console.Write(str);
                    stringBuilder.Append(str);
                    count = sr.Read(read, 0, 256);
                }
            }
            catch (Exception e)
            {
                Logger.WriteLine(e.ToString());
                stringBuilder.Append(e.Message);
                Logger.WriteLine("\r\nThe request URI could not be found or was malformed");
                throw new Exception(url + " POSTDATA=" + payload + " " + e.Message);
            }
            finally
            {
                if (result != null)
                {
                    result.Close();
                }
            }
            stringBuilder.Replace("\r", "");
            string[] rval = stringBuilder.ToString().Split(new char[] { '\n' });

            if (useCache)
            {
                SimpleWebCache.Save(url + payload, rval);
            }
            return(rval);
        }