Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

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();
ASKER CERTIFIED SOLUTION
Avatar of rdivilbiss
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers