Question

My goal is to login at http://uk.advfn.com using my application, I am novice in C#, I learnt about the below code from another link, but I cannot manage to work out mine. When I debug, the response shows bad login page rather than login successful page. Can anyone kind to look into it for me where did I do wrongly?

I use Tamper Data firefox addon to obtain those needed values, but I am not sure if I use them correctly.

Your help is very much appreciated! Thank you. :)

Part 1:

        public class CookieAwareWebClient : WebClient
    {
        public string Method;
        public CookieContainer CookieContainer { get; set; }
        public Uri Uri { get; set; }

        public CookieAwareWebClient()
            : this(new CookieContainer())
        {
        }

        public CookieAwareWebClient(CookieContainer cookies)
        {
            this.CookieContainer = cookies;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = this.CookieContainer;
                (request as HttpWebRequest).ServicePoint.Expect100Continue = false;
                (request as HttpWebRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
                (request as HttpWebRequest).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                (request as HttpWebRequest).Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
                (request as HttpWebRequest).Referer = "http://uk.advfn.com/";
                (request as HttpWebRequest).KeepAlive = true;
                (request as HttpWebRequest).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                if (Method == "POST")
                {
                    (request as HttpWebRequest).ContentType = "application/x-www-form-urlencoded";
                }

            }
            HttpWebRequest httpRequest = (HttpWebRequest)request;
            httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            return httpRequest;
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];

            if (setCookieHeader != null)
            {
                //do something if needed to parse out the cookie.
                try
                {
                    if (setCookieHeader != null)
                    {
                        Cookie cookie = new Cookie(); //create cookie
                        this.CookieContainer.Add(cookie);
                    }
                }
                catch (Exception)
                {

                }
            }
            return response;
        }
    }

Part 2:

        private void btn_login_Click(object sender, EventArgs e)
    {
        var cookieJar = new CookieContainer();
        CookieAwareWebClient client = new CookieAwareWebClient(cookieJar);
        string response = client.DownloadString("http://uk.advfn.com/common/account/login");
        string postData = string.Format("redirect_url=aHR0cDovL3VrLmFkdmZuLmNvbQ%3D%3D&site=uk&login_username=demouser&login_password=demopassword");
        client.Method = "POST";
        response = client.UploadString("https://secure.advfn.com/login/secure", postData);
    }
Was it helpful?

Solution 4

I thought I would just put an update to my own question, so that it might be able to help out others.

I ended up skip all the complicated code, I used Selenium to login the website, then download the files that I needed. Selenium is indeed a powerful tool, I downloaded 900+ files using a certain interval in each download, by just using Selenium and Firefox. Special thanks to @VDohnal who left a comment to me, unfortunately I can only upvote his comment since there's no "answer" from him.

The code is exported from Selenium IDE, I altered the code a bit and added the part where I need to do my downloads.

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    private IWebDriver driver;
    private StringBuilder verificationErrors;
    private string baseURL;
    private bool acceptNextAlert = true;

    private bool IsElementPresent(By by)
    {
        try
        {
            driver.FindElement(by);
            return true;
        }
        catch (NoSuchElementException)
        {
            return false;
        }
    }

    private bool IsAlertPresent()
    {
        try
        {
            driver.SwitchTo().Alert();
            return true;
        }
        catch (NoAlertPresentException)
        {
            return false;
        }
    }

    private string CloseAlertAndGetItsText()
    {
        try
        {
            IAlert alert = driver.SwitchTo().Alert();
            string alertText = alert.Text;
            if (acceptNextAlert)
            {
                alert.Accept();
            }
            else
            {
                alert.Dismiss();
            }
            return alertText;
        }
        finally
        {
            acceptNextAlert = true;
        }
    }

    public void TheCTest()
    {
        try
        {
            driver = new FirefoxDriver();
            baseURL = "http://uk.advfn.com";
            verificationErrors = new StringBuilder();
            driver.Navigate().GoToUrl(baseURL + "/common/account/login");
            driver.FindElement(By.Id("login_username")).Clear();
            driver.FindElement(By.Id("login_username")).SendKeys("demouser");
            driver.FindElement(By.Id("login_password")).Clear();
            driver.FindElement(By.Id("login_password")).SendKeys("demopass");
            driver.FindElement(By.Id("login_submit")).Click();
            Thread.Sleep(30000);
            //driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60));
            foreach (DataGridViewRow row in dataGridView_FetchTickers.Rows)
            {
                if (row.Cells[1].Value != null)
                {
                    try
                    {
                        driver.Navigate().GoToUrl(baseURL + "/p.php?pid=data&daily=0&symbol=L%5E" + row.Cells[1].Value.ToString());
                        //driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60));
                        Thread.Sleep(30000);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }
                else
                {
                    //Do nothing
                }
            }
        }
        catch (Exception)
        {
            //
        }  
    }

    private void btn_fetchSharePrices_Click(object sender, EventArgs e)
    {
        TheCTest();
    }
}

OTHER TIPS

I would use fiddler to capture the http request of a successful login done through the browser and then compare that to the request generated by the application. Any discrepancies in the two may hold clues as to why the application is unable to login successfully.

I did some remote login to a website a few years ago and I have saved the StackOverflow Question that helped me.

Login to website, via C#

I couldn't find my code because I'm not working at the same company I used to but hope this helps...

I've tried your code and it works fine. The only thing i had to do is to change password and login to correct one.

I guess the reason you have 'bad logon' message is because you actually entered bad login and password.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top