<p>I need to automate a process involving a website that is using a login form. I need to capture some data in the pages following the login page.</p>

<p>I know how to screen-scrape normal pages, but not those behind a secure site.</p>

<ol>
<li>Can this be done with the .NET WebClient class? 

<ul>
<li>How would I automatically login?</li>
<li>How would I keep logged in for the other pages?</li>
</ul></li>
</ol>

[{'answer_id': 48228, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 1, 'selected': False, 'text': '<p>You can easily simulate user input. You can submit form on the web page from you program by sending post\\get request to a website.<br>\nTypical login form looks like:</p>\n\n<pre><code>&lt;form name="loginForm" method="post" Action="target_page.html"&gt;\n   &lt;input type="Text" name="Username"&gt;\n   &lt;input type="Password" name="Password"&gt;\n&lt;/form&gt;\n</code></pre>\n\n<p>You can send a post request to the website providing values for Username &amp; Password fields. What happens after you send your request is largely depends on a website, usually you will be redirected to some page. You authorization info will be stored in the sessions\\cookie. So if you scrape client can maintain web session\\understands cookies you will be able to access protected pages.</p>\n\n<p>It\'s not clear from your question what language\\framework you\'re going to use. For example there is a framework for screen scraping (including login functionality) written in perl - <a href="http://search.cpan.org/dist/WWW-Mechanize/" rel="nofollow noreferrer">WWW::Mechanize</a>  </p>\n\n<p>Note, that you can face some problems if site you\'re trying to login to uses java scripts or some kind of CAPTCHA.</p>\n'}, {'answer_id': 48232, 'author': 'Nick Stinemates', 'author_id': 4960, 'author_profile': 'https://Stackoverflow.com/users/4960', 'pm_score': 0, 'selected': False, 'text': '<p>Can you please clarify? Is the WebClient class you speak of the one in HTTPUnit/Java?</p>\n\n<p>If so, your session should be saved automatically.</p>\n'}, {'answer_id': 48234, 'author': 'Hafthor', 'author_id': 4489, 'author_profile': 'https://Stackoverflow.com/users/4489', 'pm_score': 4, 'selected': True, 'text': '<p>One way would be through automating a browser -- you mentioned WebClient, so I\'m guessing you might be referring to WebClient in .NET.<p>Two main points:<ul><li>There\'s nothing special about https related to WebClient - it just works</li><li>Cookies are typically used to carry authentication -- you\'ll need to capture and replay them</li></ul></p>\n\n<p>Here\'s the steps I\'d follow:<ol><li>GET the login form, capture the the cookie in the response.</li><li>Using Xpath and HtmlAgilityPack, find the "input type=hidden" field names and values.</li><li>POST to login form\'s action with user name, password, and hidden field values in the request body. Include the cookie in the request headers. Again, capture the cookie in the response.</li><li>GET the pages you want, again, with the cookie in the request headers.</li></ol></p>\n\n<p>On step 2, I mention a somewhat complicated method for automating the login. Usually, you can post with username and password directly to the known login form action without getting the initial form or relaying the hidden fields. Some sites have form validation (different from field validation) on their forms which makes this method not work.<p><a href="http://www.codeplex.com/htmlagilitypack" rel="nofollow noreferrer">HtmlAgilityPack</a> is a .NET library that allows you to turn ill-formed html into an XmlDocument so you can XPath over it. Quite useful.<p>Finally, you may run into a situation where the form relies on client script to alter the form values before submitting. You may need to simulate this behavior.<p>Using a tool to view the http traffic for this type of work is extremely helpful - I recommend <a href="http://www.blunck.se/iehttpheaders.html" rel="nofollow noreferrer">ieHttpHeaders</a>, <a href="http://www.fiddler2.com" rel="nofollow noreferrer">Fiddler</a>, or <a href="http://getfirebug.com/" rel="nofollow noreferrer">FireBug</a> (net tab).</p>\n'}, {'answer_id': 48243, 'author': 'Andrew Whitehouse', 'author_id': 4890, 'author_profile': 'https://Stackoverflow.com/users/4890', 'pm_score': 0, 'selected': False, 'text': '<p>It isn\'t clear from your question which WebClient class (or language) you are referring to. </p>\n\n<p>If have a Java Runtime you can use the Apache HttpClient class; here\'s an example I wrote using Groovy that accesses the delicious API over SSL:</p>\n\n<pre><code>   def client = new HttpClient()\n\n   def credentials = new UsernamePasswordCredentials( "username", "password" )\n   def authScope = new AuthScope("api.del.icio.us", 443, AuthScope.ANY_REALM)\n   client.getState().setCredentials( authScope, credentials )\n\n   def url = "https://api.del.icio.us/v1/posts/get"\n\n   def method = new PostMethod( url )\n   method.addParameter( "tag", tag )\n   client.executeMethod( method )\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/48224', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1583/']