0

I'm trying to build c# application, which notify me when there is an "update" in site. The site login form contains 3 textboxes, and it's login.aspx.

My question is, how can I "send" the 3 details to the site and connect(authenticate) from the application I want to build in c#, and if it's possible, how can I do it? I looked for any guide or something to read about this but haven't found.

dove
  • 20,469
  • 14
  • 82
  • 108
user1840196
  • 53
  • 1
  • 1
  • 6
  • you need to simulate web-browser's behavior, such as: authentication, cookie, and redirection. so the WebClient class may help you – pylover Nov 20 '12 at 21:42

2 Answers2

0

you need to use the WebClient class. More info on this class can be found at http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.80).aspx

And a nice example at http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.80).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-4

DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
  • Thanks, But there is a problem. I understand that I need to know the parameters that the page sends, but when I tried with sniffer I see that the content is encrypted to a lot of letters. How can do this? – user1840196 Nov 20 '12 at 22:37
0

First you need post a form using c#

HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            Console.WriteLine (readStream.ReadToEnd ());
            response.Close ();
            readStream.Close ();

then try to save cookie, its required to store aspnet_session_id into client for future requests

private class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
        : this(new CookieContainer())
    { }
    public CookieAwareWebClient(CookieContainer c)
    {
        this.CookieContainer = c;
    }
    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
        }
        return request;
    }
}

ensure you send the and restore aspnet_session_id on each request. And bingo!!

I recommend you to read this.

Community
  • 1
  • 1
pylover
  • 7,670
  • 8
  • 51
  • 73