473,508 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form Login Help

233 New Member
I am attempting to use WebRequest to access a page that requires a login/password to access. My last WebRequest continues to timeout. Any help or thoughts would be appreciated.

Expand|Select|Wrap|Line Numbers
  1.  
  2. namespace FormsAuthTest
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. HttpWebRequest request = null;
  9. HttpWebResponse response = null;
  10. StreamReader sr = null;
  11. string originalUri = https://springboard.4imprint.com/PO/...112-46E9573F62;
  12. System.Diagnostics.ConsoleTraceListener trace =
  13. new System.Diagnostics.ConsoleTraceListener();
  14. //
  15. // Request page protected by forms authentication.
  16. // This request will get a 302 to login page
  17. //
  18. trace.Write("Requesting : " + originalUri);
  19. request = (HttpWebRequest)WebRequest.Create(originalUri);
  20. request.CookieContainer = new CookieContainer();
  21. request.AllowAutoRedirect = false;
  22. response = (HttpWebResponse)request.GetResponse();
  23. if (response.StatusCode == HttpStatusCode.Found)
  24. {
  25. trace.Write("Response: 302 ");
  26. trace.WriteLine(response.StatusCode);
  27. }
  28. else
  29. {
  30. trace.Fail("Response status is " + response.StatusCode + ". Expected was Found");
  31. }
  32. //
  33. // Get the url of login page from location header
  34. //
  35. String locationHeader = response.GetResponseHeader("Location");
  36. trace.WriteLine("Location header is " + locationHeader);
  37. trace.WriteLine("");
  38. //
  39. // Request login page
  40. //
  41. String loginPageUrl = "https://springboard.4imprint.com" + locationHeader;
  42. Console.WriteLine("Requesting " + loginPageUrl);
  43. request = (HttpWebRequest)WebRequest.Create(loginPageUrl);
  44. request.CookieContainer = new CookieContainer();
  45. request.AllowAutoRedirect = false;
  46. response = (HttpWebResponse)request.GetResponse();
  47. if (response.StatusCode == HttpStatusCode.OK)
  48. {
  49. trace.Write("Response: 200 ");
  50. trace.WriteLine(response.StatusCode);
  51. }
  52. else
  53. {
  54. trace.Fail("Response status is " + response.StatusCode + ". Expected was OK");
  55. }
  56. trace.WriteLine("Parsing login page to create post message");
  57. trace.WriteLine("");
  58. sr = new StreamReader(response.GetResponseStream());
  59. String loginResponse = sr.ReadToEnd();
  60. sr.Close();
  61. String eventTargetVar = "__EVENTTARGET=";
  62. String eventTargetValue = "";
  63. String eventArgumentVar = "__EVENTARGUMENT=";
  64. String eventArgumentValue = "";
  65. String viewStateVar = "__VIEWSTATE=";
  66. String viewStateSearchString = "name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"";
  67. int viewStateStartIndex = loginResponse.IndexOf(viewStateSearchString);
  68. loginResponse = loginResponse.Substring(viewStateStartIndex + viewStateSearchString.Length);
  69. String viewStateValue = Uri.EscapeDataString(
  70. loginResponse.Substring(0, loginResponse.IndexOf("\" />"))
  71. );
  72. loginResponse = loginResponse.Substring(loginResponse.IndexOf("\" />"));
  73. String lcSearchStr = "input name=";
  74. int lcSearchIndex = 0;
  75. //
  76. // Look for logon control id
  77. // Use any valid username and password
  78. //
  79. lcSearchIndex = loginResponse.IndexOf(lcSearchStr);
  80. loginResponse = loginResponse.Substring(lcSearchIndex + lcSearchStr.Length + 1);
  81. String userNameVar = Uri.EscapeDataString(
  82. loginResponse.Substring(0, loginResponse.IndexOf("\""))
  83. ) + "=";
  84. String userNameValue = "username";
  85. lcSearchIndex = loginResponse.IndexOf(lcSearchStr);
  86. loginResponse = loginResponse.Substring(lcSearchIndex + lcSearchStr.Length + 1);
  87. String passwordVar = Uri.EscapeDataString(
  88. loginResponse.Substring(0, loginResponse.IndexOf("\""))
  89. ) + "=";
  90. String passwordValue = "password";
  91. lcSearchStr = "type=\"submit\" name=";
  92. lcSearchIndex = loginResponse.IndexOf(lcSearchStr);
  93. loginResponse = loginResponse.Substring(lcSearchIndex + lcSearchStr.Length + 1);
  94. String loginButtonVar = Uri.EscapeDataString(
  95. loginResponse.Substring(0, loginResponse.IndexOf("\""))
  96. ) + "=";
  97. String loginButtonValue = "Log+In";
  98. String eventValidationVar = "__EVENTVALIDATION=";
  99. String eventValSearchString =
  100. "name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"";
  101. int eventValStartIndex = loginResponse.IndexOf(eventValSearchString);
  102. loginResponse = loginResponse.Substring(eventValStartIndex + eventValSearchString.Length);
  103. String eventValidationValue =
  104. Uri.EscapeDataString(
  105. loginResponse.Substring(0, loginResponse.IndexOf("\" />"))
  106. );
  107. String postString = eventTargetVar + eventTargetValue;
  108. postString += "&" + eventArgumentVar + eventArgumentValue;
  109. postString += "&" + viewStateVar + viewStateValue;
  110. postString += "&" + userNameVar + userNameValue;
  111. postString += "&" + passwordVar + passwordValue;
  112. postString += "&" + loginButtonVar + loginButtonValue;
  113. postString += "&" + eventValidationVar + eventValidationValue;
  114. //
  115. // Do a POST to login.aspx now
  116. //
  117. Console.WriteLine("POST request to https://springboard.4imprint.com" + locationHeader);
  118. request = (HttpWebRequest)WebRequest.Create("https://springboard.4imprint.com" + locationHeader);
  119. request.CookieContainer = new CookieContainer();
  120. request.AllowAutoRedirect = false;
  121. request.Method = "POST";
  122. request.ContentType = "application/x-www-form-urlencoded";
  123. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  124. byte[] requestData = encoding.GetBytes(postString);
  125. request.ContentLength = requestData.Length;
  126. Stream requestStream = request.GetRequestStream();
  127. requestStream.Write(requestData, 0, requestData.Length);
  128. requestStream.Close();
  129. response = (HttpWebResponse)request.GetResponse();
  130. if (response.StatusCode == HttpStatusCode.Found)
  131. {
  132. trace.Write("Response: 302 ");
  133. trace.WriteLine(response.StatusCode);
  134. }
  135. else
  136. {
  137. trace.Fail("Response status is " + response.StatusCode + ". Expected was Found");
  138. }
  139. locationHeader = response.GetResponseHeader("Location");
  140. trace.WriteLine("Location header is " + locationHeader);
  141. CookieCollection cookies = response.Cookies;
  142.  
  143.  
  144. //
  145.  
  146. // Send request to originalUri with the cookie
  147. // We should be able to see originalUri contents
  148. //
  149. trace.WriteLine("Requesting https://springboard.4imprint.com" + locationHeader + " with cookie");
  150. request = (HttpWebRequest)WebRequest.Create("https://springboard.4imprint.com" + locationHeader);
  151. request.CookieContainer = new CookieContainer();
  152. foreach(Cookie oneCookie in cookies)
  153. {
  154. request.CookieContainer.Add(oneCookie);
  155. }
  156. request.AllowAutoRedirect = false;
  157. //request.Headers.Add(HttpRequestHeader.Cookie, cookie);
  158. response = (HttpWebResponse)request.GetResponse();
  159. if (response.StatusCode == HttpStatusCode.OK)
  160. {
  161. trace.Write("Response: 200 ");
  162. trace.WriteLine(response.StatusCode);
  163. }
  164. else
  165. {
  166. trace.Fail("Response status is " + response.StatusCode + ". Expected was OK");
  167. }
  168. trace.WriteLine("");
  169. trace.WriteLine("Contents of " + originalUri);
  170. trace.WriteLine("");
  171. sr = new StreamReader(response.GetResponseStream());
  172. trace.WriteLine(sr.ReadToEnd());
  173. sr.Close();
  174. Console.Read();
  175. }
  176. }
  177. }
  178.  
Jan 21 '09 #1
5 3174
Plater
7,872 Recognized Expert Expert
Hmm. Are you remembering to add the Session cookie as well? Its not a regular cookie but a special header in the request.

Other then that I could only guess that you should get a packet watcher and see what data is being sent back and forth?
Jan 21 '09 #2
mcfly1204
233 New Member
You are indeed correct, the session cookie was not being stored. I will have to look into how I can save the cookies from each response object and then add them to my final request.
Jan 21 '09 #3
mcfly1204
233 New Member
So I have added the following to each HttpWebRequest:

Expand|Select|Wrap|Line Numbers
  1. request.CookieContainer = new CookieContainer();
I now have one CookieCollection that saves the session cookie, and another CookieCollection that saves two cookies to authenticate to the site. I then iterate through each CookieCollection, adding each cookie to the final request as such:

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach (oneCookie in myCollection)
  3. {
  4. request.CookieContainer.Add(oneCookie
  5. }
  6.  
I can view each cookie, but I cannot get the final response that I am looking for.
Jan 21 '09 #4
Plater
7,872 Recognized Expert Expert
When you packet watch, do they all show up in the transfer?
Jan 21 '09 #5
mcfly1204
233 New Member
Going to have to read about packet sniffing, and then I'll get back to you.
Jan 21 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
8750
by: Dave Martin | last post by:
I've successfully used CURL to maneuver around and through sites but a new site I've been trying to work with has got me stumped. To retrieve the data I'm trying to get a login is required. If...
4
1921
by: Fabio Negri Cicotti | last post by:
Hi. Imagine a scene where the user input the username and password in a "Login" form and when press the confirm button, another window is opened with the "Main" form. After that, the "Login"...
9
1945
by: Justin Engelman | last post by:
Hi, I have a website that uses an ISAPI filter that will redirect anyone going to any page on the site to an SSL login page (on a different website with the same root domain) if they do not have...
15
2264
by: carr4895 | last post by:
Hello. I was wondering if someone could help me too with a login form. Upon startup, I have to display a password screen and it should accept a user name and password. User name can be anything...
10
1533
by: Partha Protim Roy | last post by:
Hello, My problem is: I got a Login form, so once the user enter vaild Username & Password another form opens. How do I close the login form?
14
3744
by: Simon Abolnar | last post by:
I would like to know how to open child form from dialog form. Thanks for help! Simon
5
2316
by: mouac01 | last post by:
I'm new to PHP/Javascript. I have a simple form I want to validate the fields with javascript and then run the PHP script. All the scripts are in one page. I want PHP to control where the next...
5
17676
by: Navillus | last post by:
Hey gang, I have a login form that is empty by default, but can be filled with values from a previous form: <input type=text maxlength="40" size="40" name="user" value="`usr`"> <input...
4
3599
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
13
3221
JodiPhillips
by: JodiPhillips | last post by:
G'day, I have a silly and simple problem that I need some guidance with. Due to the way our network is set up, I am unable to use the group permissions for Access and have had to implement log...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.