troubleshooting Question

use existing IE cookie for website credentials with HTTPWebRequest/Response

Avatar of mikegrad7
mikegrad7Flag for United States of America asked on
C#Web Applications
5 Comments1 Solution1548 ViewsLast Modified:
Hi, first let me start by saying a am a dotnet developer, but not with anything web, so i dont have alot of expertise in this area.

I am trying to write a program that gets the source of an external  web page. however i need to login credentials or else i dont see the page i want. Right now, even though IE has a cookie and my info is saved on the site, looking at the src from the web request i can see i am not getting the logged in data.

From this site and the web i found some code but it isnt working for me. Im not sure i did the post piece correctly. I used Fiddler to try and match the post data, but i am not seeing what i expect.

It seems easier to just use the existing cookie, since i can identify it on my system, but i havent figured out how to do that either.  Any help would be appreciated.


and here is the src from the page, where the login section is:
<div id="shell">
      <form id="login" class="hideLogin" method="post" action="/login.aspx">
          <div>
                
                      
                      
                      <div class="input_left"></div><input type="text" id="username" name="username" class="cover" value="Username" /><div class="input_right"></div>                        
                        <div id="password1" class="password_container"><div class="input_left"></div><input type="text" id="pwdText" name="pwdText" value="Password" class="password" /><div class="input_right"></div></div>

                        <div id="password2" class="password_container"><div class="input_left"></div><input type="password" id="password" name="password" value="" class="password" /><div class="input_right"></div></div>
                        <!--<input type="password" id="password" name="password" value="Password" class="password"/>-->
                        <input type="image" src="/images/shell/login/signin.gif" alt="Signin" id="user_pass_btn" />        
                        <input type="checkbox" name="remenber" id="remenber" value="Remenber" />
                        <label for="remenber">Remember Me</label>
                        <a href="/forgotpassword.aspx" title="Forgot your Password?" id="forget">Forgot your Password?</a>
                        <a href="/registrationoptions.aspx" title="Register?" id="register">Register</a>
                        <input type="hidden" id="referer" name="referer" value="/refererpage">
                        
        </div>
    </form>
   
// Code copied
                string UserName = "someemail@yahoo.com";
                string Password = "upassword";
                string LoginUrl = "http://www.somewebpage.com/login.aspx";
               
                ASCIIEncoding encoding = new ASCIIEncoding();
                string postData = "username=" + UserName;
                postData += ("&pwdText=Password");
                postData += ("&password=" + Password);
                postData += ("&referer=/Default.aspx");
                //postData += ("&__VIEWSTATE=" + "dDwxNDk4NTExNDg3OztsPFBlcnNpc3Q7Pj6Eyncow4uVa/NxzavPfMvSqZKDFg==");
                //postData += ("&Submit1=Log On");
                byte[] data = encoding.GetBytes(postData);

                System.Net.Cookie c = new Cookie("DUMMY", "");

                Uri u = new Uri(LoginUrl);

                System.Net.HttpWebRequest w = (HttpWebRequest)System.Net.WebRequest.Create(u);

                w.CookieContainer = new CookieContainer();
                w.CookieContainer.Add(u, c);
                w.Referer = "http://www.somewebpage.com/Default.aspx";

                w.Method = "POST";
                w.ContentType = "application/x-www-form-urlencoded";
                w.ContentLength = data.Length;
                Stream newStream = w.GetRequestStream();
                // Send the data.
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                w.Credentials = System.Net.CredentialCache.DefaultCredentials;
                string str = "";

	
//end copied code
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.somewebside.com/webpage");
                webRequest.Timeout = 60000;

                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                Stream responseStream = webResponse.GetResponseStream();

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros