2

So I've been trying to fetch a web page that uses authentication to a string and save it to a file. It should be pretty basic so I hope someone can see my errors. I'm very new to C# so treat me thereafter :)

This code functions to some extend, but the file I get is the html for the login screen and not the page that its shown for users that is logged in. What is it I'm doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace fetchingweb
{
    class WebAutheticator
    {
        static void Main(string[] args)
        {
            string htmlHer = GetWeb();
            StreamWriter file = new
                StreamWriter("C:\\Users\\user\\Documents\\atextfile.txt");
            file.Write(htmlHer);
            file.Close();
        } //main end

        private static string GetWeb()
        {
            WebClient wc = new WebClient();
            wc.Credentials = new NetworkCredential("user", "pass");
            string url = "http://someurl.com/index.php";
            try
            {
                using (Stream stream = wc.OpenRead(new Uri(url)))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (WebException e)
            {
                return "failure!";
            }
        } //getweb end
    } //class end
} //namespace end
Koryun
  • 58
  • 1
  • 8
Shogoot
  • 297
  • 2
  • 6
  • 13
  • Does the web page do the nasty dialogbox popup login, or, a form on the web page based login? – BugFinder Aug 04 '11 at 12:07
  • No popup, but it has a form to enter the user and pass, one of those standard boxes. – Shogoot Aug 04 '11 at 12:22
  • Then almost certainly its not using the built in web authentification.. Which would stop this working - you can prove that by using your local IIS you get with vis studio, and having a web auth page. – BugFinder Aug 04 '11 at 12:46
  • I dont quite get what you are saying. please elaborate. – Shogoot Aug 04 '11 at 14:59

2 Answers2

2

You are using NetworkCredential to login at the webapp, but the webapp is using some kind of forms authentication. As long as the webapp is not configured to use network credentials this will not work.

Since this is a php application I guess it uses plain forms auth and that you will need to post username/password to the login page before continuing.

3komma14
  • 168
  • 1
  • 7
  • any suggestion on how to do this? please point me in the right direction or post here a stackoverflow thread that addresses the issue :) – Shogoot Aug 04 '11 at 15:01
  • lokk[link] (http://stackoverflow.com/questions/4740752/how-to-login-with-webclient-c) – 3komma14 Aug 04 '11 at 16:38
  • Look at the at the previous link and this one (http://stackoverflow.com/questions/726710/fake-a-form-submission-with-c-webclient). – 3komma14 Aug 04 '11 at 16:53
0

Look at the code on the login page of the site you're trying to log into and download. There will be a <FORM..> section, if its post you need to post, if its get you use get. It will either post to a new page, and possibly use redirection, or, even refer to itself. Chances are it will use either some form of session id, or cookies to prove you logged in.

Best way to understand this is to write a proxy, which takes your requests, and shows what you send and what you get back.. As this is the best way to understand what happens when you use things and what your program would need to do to mimic it

BugFinder
  • 17,474
  • 4
  • 36
  • 51