CreateRequest() public static method

public static CreateRequest ( string address ) : HttpWebRequest
address string
return System.Net.HttpWebRequest
Ejemplo n.º 1
0
        private static string GetPostContents(int postId)
        {
            Console.WriteLine("Reading post {0}", postId);

            var request = Scraper.CreateRequest(String.Format("http://facepunch.com/ajax.php?do=getquotes&p={0}", postId));

            request.Method = "POST";

            var values = new NameValueCollection();

            values["do"] = "getquotes";
            values["p"]  = postId.ToString("D");

            request.ContentType = "application/x-www-form-urlencoded";

            using (var stream = new StreamWriter(request.GetRequestStream())) {
                stream.Write("do=getquotes&p={0}", postId);
            }

            var text = String.Empty;

            using (var response = request.GetResponse()) {
                using (var stream = new StreamReader(response.GetResponseStream(), Program.FacepunchEncoding)) {
                    text = stream.ReadToEnd();
                }
            }

            var lines = text.Split('\n');

            var result = new StringBuilder();

            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];

                if (i == 0 || i >= lines.Length - 3)
                {
                    continue;
                }

                if (i == 1)
                {
                    result.Append(line.Substring(17));
                }
                else
                {
                    result.Append(line);
                }

                result.Append('\n');
            }

            return(result.ToString().Trim());
        }
Ejemplo n.º 2
0
        private static string GetPostContents(int postId)
        {
            Console.WriteLine("Reading post {0}", postId);

            var request = Scraper.CreateRequest(string.Format("http://facepunch.com/ajax.php?do=getquotes&p={0}", postId));

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            request.Headers["Origin"]           = "http://facepunch.com";
            request.Headers["X-Requested-With"] = "XMLHttpRequest";
            request.Referer = string.Format(Scraper.ThreadPostString, Program.ThreadId, postId);

            var values = new NameValueCollection();

            values["do"]            = "getquotes";
            values["p"]             = postId.ToString("D");
            values["securitytoken"] = Program.Config.Authentication.SecurityToken;

            var postDataBuilder = new StringBuilder();
            var trail           = string.Empty;

            foreach (string key in values.AllKeys)
            {
                postDataBuilder.Append(trail);
                postDataBuilder.Append(HttpUtility.UrlEncode(key));
                postDataBuilder.Append("=");
                postDataBuilder.Append(HttpUtility.UrlEncode(values[key]));
                trail = "&";
            }

            var postData = Encoding.UTF8.GetBytes(postDataBuilder.ToString());

            request.ContentLength = postData.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(postData, 0, postData.Length);
            }

            string text;

            using (var response = request.GetResponse())
            {
                using (var stream = new StreamReader(response.GetResponseStream(), Program.FacepunchEncoding))
                {
                    text = stream.ReadToEnd();
                }
            }

            var lines = text.Split('\n');

            var result = new StringBuilder();

            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];

                if (i == 0 || i >= lines.Length - 3)
                {
                    continue;
                }

                if (i == 1)
                {
                    result.Append(line.Substring(17));
                }
                else
                {
                    result.Append(line);
                }

                result.Append('\n');
            }

            return(result.ToString().Trim());
        }