locked
Navigate Webpage From Within Another Webpage RRS feed

  • Question

  • User1005168252 posted

    Hello,

    I have been trying to pass in an ID Number, and Password into another pages login screen and open the authenticated page in a window in which I can parse.  Not sure if I am making sence. 

    For Example:  open hotmail and return all the email titles or something like that...

    I have been trying the NetworkCredential method, and the  WebRequest.Method = "POST"

    The page I am trying to open has two passwords, and other then the textbox id's I am not sure how to access them from ASP.Net.

    Thanks for any help.

    CE

    Wednesday, May 29, 2013 3:54 PM

All replies

  • User1577908161 posted

    I use a custom class callsed SPWebSession. You can use this to post username and password to a URL.

    The same code to call it is displayed below.

    NameValueCollection postcl = new NameValueCollection();

    session = new SPWebSession();

    postcl.Add("user_name", stringwithusername );
    postcl.Add("password", stringwithpassword );
    postcl.Add("NextPage", "homepage.aspx");

    string input = session.posturlR("www.myusersite.com", postcl);

    using System;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Collections;
    using System.Text.RegularExpressions;
    using System.Web;
     
    
    
    namespace WindowsFormsApplication1
    {
    
        public class SPWebSession
        {
            // Fields
            private bool ar;
            private CookieCollection cc;
            public NameValueCollection cookienv;
            public string cookiestring;
            public bool coverride;
            private string ct;
            private string data;
            private bool ka;
            private string location;
            private string mt;
            private NameValueCollection pd;
            private byte[] postbytes;
            private string poststr;
            private Stream poststream;
            private string redirect;
            private bool redirected;
            private WebRequest request;
            private WebResponse response;
            private StreamReader sr;
            private Stream str;
            private string ua;
    
            // Methods
            public SPWebSession()
            {
                this.cookienv = new NameValueCollection();
                this.cookiestring = "";
                this.coverride = false;
                this.ka = false;
                this.ar = true;
                this.ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
                this.ct = "";
                this.mt = "";
                this.cc = new CookieCollection();
                this.cookiestring = "";
                this.cookienv = new NameValueCollection();
            }
    
            public SPWebSession(string url)
            {
                this.cookienv = new NameValueCollection();
                this.cookiestring = "";
                this.coverride = false;
                this.ka = false;
                this.ar = true;
                this.ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
                this.ct = "";
                this.mt = "";
                this.cc = new CookieCollection();
                this.location = url;
                this.cookiestring = "";
                this.cookienv = new NameValueCollection();
            }
    
            public string geturl(string url)
            {
                this.request = (HttpWebRequest)WebRequest.Create(url);
                ((HttpWebRequest)this.request).KeepAlive = this.ka;
                ((HttpWebRequest)this.request).AllowAutoRedirect = this.ar;
                ((HttpWebRequest)this.request).UserAgent = this.ua;
                if (this.coverride)
                {
                    ((HttpWebRequest)this.request).CookieContainer = new CookieContainer();
                    ((HttpWebRequest)this.request).CookieContainer.Add(this.cc);
                }
                else
                {
                    ((HttpWebRequest)this.request).Headers.Add("Cookie", this.cookiestring);
                }
                ((HttpWebRequest)this.request).ProtocolVersion = HttpVersion.Version10;
                ((HttpWebRequest)this.request).Referer = this.location;
                this.response = this.request.GetResponse();
                this.str = this.response.GetResponseStream();
                this.location = this.response.ResponseUri.ToString();
                this.sr = new StreamReader(this.str, Encoding.UTF8);
                this.data = this.sr.ReadToEnd();
                if (this.response.Headers["Location"] == null)
                {
                    this.redirected = false;
                }
                else
                {
                    string text1 = this.response.Headers["Set-Cookie"];
                    this.redirect = this.response.Headers["Location"];
                    if (!this.redirect.StartsWith("http"))
                    {
                        this.redirect = "http://" + this.response.ResponseUri.Host + this.redirect;
                    }
                }
                if (this.response.Headers["Set-Cookie"] != null)
                {
                    this.keepcookies(this.response.Headers["Set-Cookie"]);
                }
                this.request.Abort();
                this.request = null;
                this.response.Close();
                this.response = null;
                this.str.Close();
                this.str = null;
                this.sr.Close();
                this.sr = null;
                return this.data;
            }
    
            public string geturlR(string url)
            {
                this.ar = false;
                this.redirected = true;
                this.data = this.geturl(url);
                while (this.redirected)
                {
                    this.data = this.geturl(this.redirect);
                }
                this.ar = true;
                return this.data;
            }
    
            public void keepcookies(string cookiestr)
            {
                int num;
                Regex regex = new Regex("([^=]*)=(.*)", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                string[] strArray = cookiestr.Split(new char[] { ',' });
                int num2 = strArray.Length - 1;
                for (num = 0; num <= num2; num++)
                {
                    string str2 = strArray[num];
                    str2 = str2.Trim();
                    if (str2.IndexOf('=') > 0)
                    {
                        string[] strArray2 = str2.Split(new char[] { ';' });
                        string input = strArray2[0];
                        Match match = regex.Match(input);
                        if (match.Length != 0)
                        {
                            string name = match.Groups[1].Value;
                            string str4 = match.Groups[2].Value;
                            if (this.cookienv[name] == null)
                            {
                                this.cookienv.Add(name, str4);
                            }
                            else
                            {
                                this.cookienv[name] = str4;
                            }
                            Cookie cookie = new Cookie(name, str4);
                            foreach (string str5 in strArray2)
                            {
                                string[] strArray3 = str5.Split(new char[] { '=' });
                                if (strArray3[0].Trim().ToLower().Equals("domain"))
                                {
                                    cookie.Domain = strArray3[1].Trim();
                                }
                                else if (strArray3[0].Trim().ToLower().Equals("path"))
                                {
                                    cookie.Path = strArray3[1].Trim();
                                }
                            }
                            if ((cookie.Domain == null) | cookie.Domain.Equals(""))
                            {
                                cookie.Domain = new Uri(this.location).Host;
                            }
                            if ((cookie.Path == null) | cookie.Path.Equals(""))
                            {
                                cookie.Path = "/";
                            }
                            cookie.Version = 0;
                            this.cc.Add(cookie);
                            cookie.Discard = true;
                            cookie = null;
                        }
                    }
                }
                cookiestr = "";
                int num4 = this.cookienv.Count - 1;
                for (num = 0; num <= num4; num++)
                {
                    cookiestr = cookiestr + this.cookienv.GetKey(num).ToString() + "=" + this.cookienv[this.cookienv.GetKey(num).ToString()] + "; ";
                }
                this.cookiestring = cookiestr;
            }
    
            public string posturl(string url, NameValueCollection postcl)
            {
                this.postdata = postcl;
                this.request = (HttpWebRequest)WebRequest.Create(url);
                this.request.ContentType = "application/x-www-form-urlencoded";
                this.request.Method = "POST";
                ((HttpWebRequest)this.request).Referer = this.location;
                ((HttpWebRequest)this.request).KeepAlive = this.ka;
                ((HttpWebRequest)this.request).UserAgent = this.ua;
                if (this.coverride)
                {
                    ((HttpWebRequest)this.request).CookieContainer = new CookieContainer();
                    ((HttpWebRequest)this.request).CookieContainer.Add(this.cc);
                }
                else
                {
                    ((HttpWebRequest)this.request).Headers.Add("Cookie", this.cookiestring);
                }
                ((HttpWebRequest)this.request).ProtocolVersion = HttpVersion.Version10;
                this.request.ContentLength = this.postbytes.Length;
                this.poststream = this.request.GetRequestStream();
                this.poststream.Write(this.postbytes, 0, this.postbytes.Length);
                ((HttpWebRequest)this.request).AllowAutoRedirect = this.ar;
                this.response = this.request.GetResponse();
                this.str = this.response.GetResponseStream();
                this.location = this.response.ResponseUri.ToString();
                this.sr = new StreamReader(this.str, Encoding.UTF8);
                this.data = this.sr.ReadToEnd();
                if (this.response.Headers["Location"] == null)
                {
                    this.redirected = false;
                }
                else
                {
                    string text1 = this.response.Headers["Set-Cookie"];
                    this.redirect = this.response.Headers["Location"];
                    if (!this.redirect.StartsWith("http"))
                    {
                        this.redirect = "http://" + this.response.ResponseUri.Host + this.redirect;
                    }
                }
                if (this.response.Headers["Set-Cookie"] != null)
                {
                    this.keepcookies(this.response.Headers["Set-Cookie"]);
                }
                this.poststream.Close();
                this.poststream = null;
                this.request.Abort();
                this.request = null;
                this.response.Close();
                this.response = null;
                this.str.Close();
                this.str = null;
                this.sr.Close();
                this.sr = null;
                return this.data;
            }
    
            public string posturlR(string url, NameValueCollection postcl)
            {
                bool flag = false;
                if (this.ar)
                {
                    this.ar = false;
                    this.redirected = true;
                }
                else
                {
                    this.redirected = true;
                    flag = true;
                }
                this.data = this.posturl(url, postcl);
                while (this.redirected)
                {
                    this.data = this.geturl(this.redirect);
                    if (flag)
                    {
                        this.redirected = false;
                    }
                }
                this.ar = true;
                return this.data;
            }
    
            public string posturlstr(string url, string poststr)
            {
                this.postbytes = Encoding.UTF8.GetBytes(poststr);
                this.request = WebRequest.Create(url);
                this.request.ContentType = "application/x-www-form-urlencoded";
                this.request.Method = "POST";
                ((HttpWebRequest)this.request).Expect = null;
                ((HttpWebRequest)this.request).KeepAlive = true;
                ((HttpWebRequest)this.request).AllowAutoRedirect = true;
                this.request.Headers.Add("Cache-Control", "no-cache");
                ((HttpWebRequest)this.request).Headers.Add("Cookie", this.cookiestring);
                ((HttpWebRequest)this.request).UserAgent = this.ua;
                this.request.ContentLength = this.postbytes.Length;
                this.poststream = this.request.GetRequestStream();
                this.poststream.Write(this.postbytes, 0, this.postbytes.Length);
                this.poststream.Close();
                this.poststream = null;
                this.response = this.request.GetResponse();
                this.str = this.response.GetResponseStream();
                this.location = this.response.ResponseUri.ToString();
                this.sr = new StreamReader(this.str, Encoding.UTF8);
                this.data = this.sr.ReadToEnd();
                if (this.response.Headers["Set-Cookie"] != null)
                {
                    this.keepcookies(this.response.Headers["Set-Cookie"]);
                }
                this.request.Abort();
                this.request = null;
                this.response.Close();
                this.response = null;
                this.str.Close();
                this.str = null;
                this.sr.Close();
                this.sr = null;
                return this.data;
            }
    
            public void reset()
            {
                this.cookiestring = "";
                this.cookienv.Clear();
            }
    
            // Properties
            public bool autoredirect
            {
                get
                {
                    return this.ar;
                }
                set
                {
                    this.ar = value;
                }
            }
    
            public string contenttype
            {
                get
                {
                    return this.ct;
                }
                set
                {
                    this.ct = value;
                }
            }
    
            public bool keepalive
            {
                get
                {
                    return this.ka;
                }
                set
                {
                    this.ka = value;
                }
            }
    
            public string method
            {
                get
                {
                    return this.mt;
                }
                set
                {
                    this.mt = value;
                }
            }
    
            public NameValueCollection postdata
            {
                get
                {
                    return this.pd;
                }
                set
                {
                    this.poststr = "";
                    this.pd = value;
                    foreach (string str in this.pd.AllKeys)
                    {
                        //this.poststr = this.poststr + System.Web.HttpUtility.UrlEncode(str) + "=" + HttpUtility.UrlEncode(this.pd[str]) + "&";
                        this.poststr = this.poststr + str + "=" + this.pd[str] + "&";
    
                    }
                    this.poststr = this.poststr.TrimEnd(new char[] { '&' });
                    this.postbytes = Encoding.UTF8.GetBytes(this.poststr);
                }
            }
    
            public string url
            {
                get
                {
                    return this.location;
                }
            }
    
            public string useragent
            {
                get
                {
                    return this.ua;
                }
                set
                {
                    this.ua = value;
                }
            }
        }
    
    }



    Friday, August 9, 2013 5:04 AM