PrepareWebRequest() public static method

Creates an HTTP web request. Timeout set to 15 seconds
public static PrepareWebRequest ( string url ) : HttpWebRequest
url string /// URL. ///
return System.Net.HttpWebRequest
Example #1
0
        /// <summary>
        /// Retrieves the history for an article.
        /// </summary>
        /// <param name="Article">The article that history is required for.</param>
        /// <param name="Limit">The maximum number of revisions to return.</param>
        /// <returns>The history, as a generic list of Revision objects.</returns>
        public List <Revision> GetHistory(string Article, int Limit)
        {
            string targetUrl = m_indexpath + "api.php?action=query&prop=revisions&titles=" + HttpUtility.UrlEncode(Article) +
                               "&rvlimit=" + Limit;

            HttpWebRequest wr = Variables.PrepareWebRequest(targetUrl, UserAgent);

            List <Revision> history = new List <Revision>();

            HttpWebResponse resps  = (HttpWebResponse)wr.GetResponse();
            Stream          stream = resps.GetResponseStream();
            StreamReader    sr     = new StreamReader(stream);

            string pagetext = sr.ReadToEnd();

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(pagetext);

            foreach (XmlElement rvElement in doc.GetElementsByTagName("rev"))
            {
                Revision rv = new Revision();

                rv.RevisionID = Convert.ToInt32(rvElement.Attributes["revid"].Value);
                rv.Time       = DateTime.Parse(rvElement.Attributes["timestamp"].Value);
                rv.Summary    = rvElement.InnerText;
                rv.User       = rvElement.Attributes["user"].Value;

                rv.Minor = (rvElement.OuterXml.Contains("minor=\""));

                history.Add(rv);
            }

            return(history);
        }
Example #2
0
        /// <summary>
        /// Returns all pages in user's watchlist
        /// </summary>
        /// <returns>StringCollection with page titles</returns>
        public StringCollection GetWatchlist()
        {
            HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath +
                                                            "index.php?title=Special:Watchlist/edit", UserAgent);

            wr.CookieContainer = new CookieContainer();
            foreach (Cookie cook in logincookies)
            {
                wr.CookieContainer.Add(cook);
            }

            WebResponse resps = wr.GetResponse();

            Stream       stream = resps.GetResponseStream();
            StreamReader sr     = new StreamReader(stream);

            string html = sr.ReadToEnd();

            resps.Close();
            sr.Close();

            StringCollection list = new StringCollection();
            Regex            r    = new Regex("<input type=\"checkbox\" name=\"id\\[\\]\" value=(.*?) />");

            foreach (Match m in r.Matches(html))
            {
                string title = m.Groups[1].Value.Trim('"');
                title = title.Replace("&amp;", "&").Replace("&quot;", "\"");
                list.Add(title);
            }

            return(list);
        }
Example #3
0
        /// <summary>
        /// Logs this instance in with the specified username and password.
        /// </summary>
        /// <param name="Username">The username to log in with.</param>
        /// <param name="password">The password to log in with.</param>
        public void LogIn(string Username, string password)
        {
            HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath + "index.php?title=Special:Userlogin&action=submitlogin&type=login", UserAgent);

            //Create poststring
            string poststring = string.Format("wpName=+{0}&wpPassword={1}&wpRemember=1&wpLoginattempt=Log+in",
                                              new[] { HttpUtility.UrlEncode(Username), HttpUtility.UrlEncode(password) });

            wr.Method            = "POST";
            wr.ContentType       = "application/x-www-form-urlencoded";
            wr.CookieContainer   = new CookieContainer();
            wr.AllowAutoRedirect = false;

            //poststring = HttpUtility.UrlEncode(poststring);

            byte[] bytedata = Encoding.UTF8.GetBytes(poststring);

            wr.ContentLength = bytedata.Length;

            Stream rs = wr.GetRequestStream();

            rs.Write(bytedata, 0, bytedata.Length);
            rs.Close();

            HttpWebResponse resps = (HttpWebResponse)wr.GetResponse();

            logincookies = resps.Cookies;
        }
Example #4
0
        /// <summary>
        /// Internal function to retrieve the HTML for the "edit" page of an article.
        /// </summary>
        /// <param name="Article">The article to retrieve the edit page for.</param>
        /// <returns>The full HTML source of the edit page for the specified article.</returns>
        public string GetEditPage(String Article)
        {
            HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath + "index.php?title=" +
                                                            HttpUtility.UrlEncode(Article) + "&action=edit", UserAgent);

            wr.CookieContainer = new CookieContainer();

            foreach (Cookie cook in logincookies)
            {
                wr.CookieContainer.Add(cook);
            }

            HttpWebResponse resps = (HttpWebResponse)wr.GetResponse();

            Stream       stream = resps.GetResponseStream();
            StreamReader sr     = new StreamReader(stream);

            string wikitext = sr.ReadToEnd();

            sr.Close();
            stream.Close();
            resps.Close();

            return(wikitext);
        }
Example #5
0
        /// <summary>
        /// Gets the wikitext for a specified article.
        /// </summary>
        /// <param name="Article">The article to return the wikitext for.</param>
        /// <param name="indexpath">The path to the index page of the wiki to edit.</param>
        /// <returns>The wikitext of the specified article.</returns>
        public static String GetWikiText(String Article, string indexpath, int oldid)
        {
            try
            {
                string targetUrl = indexpath + "index.php?title=" + Tools.WikiEncode(Article) + "&action=raw";

                if (oldid != 0)
                {
                    targetUrl += "&oldid=" + oldid;
                }

                HttpWebRequest  wr = Variables.PrepareWebRequest(targetUrl, mUserAgent);
                HttpWebResponse resps;
                Stream          stream;
                StreamReader    sr;
                string          wikitext = "";

                //wr.Proxy.Credentials = CredentialCache.DefaultCredentials;

                resps = (HttpWebResponse)wr.GetResponse();

                stream = resps.GetResponseStream();
                sr     = new StreamReader(stream);

                wikitext = sr.ReadToEnd();

                sr.Close();
                stream.Close();
                resps.Close();

                return(wikitext);
            }
            catch (WebException ex)
            {
                if (ex.ToString().Contains("404"))
                {
                    return("");
                }
                else
                {
                    throw;
                }
            }
        }
Example #6
0
        /// <summary>
        /// Adds the specified page to current user's watchlist or removes from it
        /// </summary>
        /// <param name="Page">Page to watch/unwatch</param>
        /// <param name="Watch">Whether to add or remove</param>
        /// <returns>true if successful</returns>
        public bool WatchPage(String Page, bool Watch)
        {
            try
            {
                HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath + "index.php?title=" +
                                                                HttpUtility.UrlEncode(Page) + "&action=" + (Watch ? "watch" : "unwatch"), UserAgent);

                wr.CookieContainer = new CookieContainer();

                foreach (Cookie cook in logincookies)
                {
                    wr.CookieContainer.Add(cook);
                }

                WebResponse resps = wr.GetResponse();
                resps.Close();
            }
            catch { return(false); }
            return(true);
        }
Example #7
0
        /// <summary>
        /// Gets the HTML from the given web address.
        /// </summary>
        /// <param name="URL">The URL of the webpage.</param>
        /// <param name="Enc">The encoding to use.</param>
        /// <returns>The HTML.</returns>
        public static string GetHTML(string URL, Encoding Enc)
        {
            string text = "";

            try
            {
                HttpWebRequest rq = Variables.PrepareWebRequest(URL); // Uses WikiFunctions' default UserAgent string

                HttpWebResponse response = (HttpWebResponse)rq.GetResponse();

                Stream       stream = response.GetResponseStream();
                StreamReader sr     = new StreamReader(stream, Enc);

                text = sr.ReadToEnd();

                sr.Close();
                stream.Close();
                response.Close();

                return(text);
            }
            catch { throw; }
        }
Example #8
0
        /// <summary>
        /// Removes all items from watchlist
        /// </summary>
        /// <returns>true if successful</returns>
        public bool ClearWatchlist()
        {   // doesn't work so far
            try
            {
                Regex rx = new Regex("<input name=\"token\" type=\"hidden\" value=\"([^\"]*)\" />");

                HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath +
                                                                "index.php?title=Special:Watchlist/clear", UserAgent);

                wr.CookieContainer = new CookieContainer();

                foreach (Cookie cook in logincookies)
                {
                    wr.CookieContainer.Add(cook);
                }

                WebResponse resps = wr.GetResponse();

                Stream       stream = resps.GetResponseStream();
                StreamReader sr     = new StreamReader(stream);

                string html = sr.ReadToEnd();
                resps.Close();
                sr.Close();

                Match m = rx.Match(html);

                int    index = m.Value.IndexOf("value=\"") + 7;
                string token = m.Value.Substring(index, m.Value.Substring(index).IndexOf("\""));

                wr = Variables.PrepareWebRequest(m_indexpath +
                                                 "index.php?title=Special:Watchlist&amp;action=clear", UserAgent);

                wr.CookieContainer = new CookieContainer();

                foreach (Cookie cook in logincookies)
                {
                    wr.CookieContainer.Add(cook);
                }

                wr.Method      = "POST";
                wr.ContentType = "application/x-www-form-urlencoded";

                byte[] bytedata = Encoding.UTF8.GetBytes("submit=Submit&token=" + token);

                wr.ContentLength = bytedata.Length;

                Stream rs = wr.GetRequestStream();

                rs.Write(bytedata, 0, bytedata.Length);
                rs.Close();

                resps  = wr.GetResponse();
                stream = resps.GetResponseStream();
                sr     = new StreamReader(stream);

                sr.ReadToEnd();
            }
            catch { return(false); }
            return(true);
        }
Example #9
0
        /// <summary>
        /// Edits the specified page.
        /// </summary>
        /// <param name="Article">The article to edit.</param>
        /// <param name="NewText">The new wikitext for the article.</param>
        /// <param name="Summary">The edit summary to use for this edit.</param>
        /// <param name="Minor">Whether or not to flag the edit as minor.</param>
        /// <param name="Watch">Whether article should be added to your watchlist</param>
        /// <returns>An EditPageRetvals object</returns>
        public EditPageRetvals EditPageEx(String Article, String NewText, String Summary, bool Minor, bool Watch)
        {
            HttpWebRequest wr = Variables.PrepareWebRequest(m_indexpath + "index.php?title=" +
                                                            Tools.WikiEncode(Article) + "&action=submit", UserAgent);

            string editpagestr = GetEditPage(Article);

            Match  m          = Edittime.Match(editpagestr);
            string wpEdittime = m.Groups[1].Value;

            m = EditToken.Match(editpagestr);
            string wpEditkey = HttpUtility.UrlEncode(m.Groups[1].Value);

            wr.CookieContainer = new CookieContainer();

            foreach (Cookie cook in logincookies)
            {
                wr.CookieContainer.Add(cook);
            }

            //Create poststring
            string poststring =
                string.Format(
                    "wpSection=&wpStarttime={0}&wpEdittime={1}&wpScrolltop=&wpTextbox1={2}&wpSummary={3}&wpSave=Save%20Page&wpEditToken={4}",
                    new[]
            {
                DateTime.Now.ToUniversalTime().ToString("yyyyMMddHHmmss"), wpEdittime,
                HttpUtility.UrlEncode(NewText), HttpUtility.UrlEncode(Summary), wpEditkey
            });

            if (Minor)
            {
                poststring = poststring.Insert(poststring.IndexOf("wpSummary"), "wpMinoredit=1&");
            }

            if (Watch || editpagestr.Contains("type='checkbox' name='wpWatchthis' checked='checked' accesskey=\"w\" id='wpWatchthis'  />"))
            {
                poststring += "&wpWatchthis=1";
            }

            wr.Method      = "POST";
            wr.ContentType = "application/x-www-form-urlencoded";

            //poststring = HttpUtility.UrlEncode(poststring);

            byte[] bytedata = Encoding.UTF8.GetBytes(poststring);

            wr.ContentLength = bytedata.Length;

            Stream rs = wr.GetRequestStream();

            rs.Write(bytedata, 0, bytedata.Length);
            rs.Close();

            WebResponse resps = wr.GetResponse();

            StreamReader    sr     = new StreamReader(resps.GetResponseStream());
            EditPageRetvals retval = new EditPageRetvals();

            retval.article = Article;

            retval.responsetext = sr.ReadToEnd();

            Match permalinkmatch = permalinkrx.Match(retval.responsetext);

            //From the root directory.
            retval.difflink = m_indexpath.Substring(0, m_indexpath.IndexOf("/", 9)) +
                              permalinkmatch.Groups[1].Value + "&diff=prev";

            retval.difflink = retval.difflink.Replace("&amp;", "&");

            // TODO: Check our submission worked and we have a valid difflink; throw an exception if not. Also check for the sorry we could not process because of loss of session data message
            return(retval);
        }