Exemplo n.º 1
0
        /// <summary>
        ///     Downloads files from the specified URL
        /// </summary>
        /// <param name="url">URL of the file</param>
        /// <param name="path">Path to save the file to</param>
        public static void Download(string url, string path)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Method    = WebRequestMethods.Http.Get;
            request.Accept    = "*/*";
            request.UserAgent = UserAgents.GetRandomUserAgent();

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (Stream fileStream = File.OpenWrite(path))
                    {
                        if (stream == null)
                        {
                            throw new Exception("Stream is empty!");
                        }

                        var buffer    = new byte[4096];
                        int bytesRead = stream.Read(buffer, 0, 4096);
                        while (bytesRead > 0)
                        {
                            fileStream.Write(buffer, 0, bytesRead);
                            bytesRead = stream.Read(buffer, 0, 4096);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Retrieves the content of the specified web page.
        /// </summary>
        /// <param name="url">Location of the web page</param>
        /// <returns>Content of the web page</returns>
        public static string Get(string url)
        {
            string content;

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

            request.Method    = WebRequestMethods.Http.Get;
            request.Accept    = "*/*";
            request.UserAgent = UserAgents.GetRandomUserAgent();
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.Headers.Add("Accept-Language: en-US,en;q=0.5");

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Stream is empty!");
                    }

                    using (var streamReader = new StreamReader(stream))
                    {
                        content = streamReader.ReadToEnd();
                    }
                }
            }

            return(content);
        }
Exemplo n.º 3
0
 public HttpOptions()
 {
     ProtocolVersion     = HttpProtocol.Version11;
     HttpMethod          = HttpMethod.Get;
     HttpAcceptHeader    = "*/*";
     UserAgent           = UserAgents.GetRandomUserAgent();
     DecompressionMethod = DecompressionMethods.GZip;
     AllowAutoRedirect   = true;
 }
Exemplo n.º 4
0
        /// <summary>
        ///     Posts data and retrieves the content of the specified web page.
        /// </summary>
        /// <param name="url">Location of the web page</param>
        /// <param name="data">Data to post to the web page</param>
        /// <returns>Content of the web page</returns>
        public static string Post(string url, string data)
        {
            string content;

            byte[] postBytes = Encoding.UTF8.GetBytes(data);

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

            request.Method    = WebRequestMethods.Http.Post;
            request.Accept    = "*/*";
            request.UserAgent = UserAgents.GetRandomUserAgent();
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType            = "application/x-www-form-urlencoded; charset=UTF-8";
            request.ContentLength          = postBytes.Length;
            request.Headers.Add("Accept-Language: en-US,en;q=0.5");

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(postBytes, 0, postBytes.Length);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Stream is empty!");
                    }

                    using (var streamReader = new StreamReader(stream))
                    {
                        content = streamReader.ReadToEnd();
                    }
                }
            }

            return(content);
        }