0

I "wrote" this code and it works very well. It is to login to a site and retrieve the data so I can see what the Bitcoin price currently is. *But it does not update After i initially logged in and retrieved data the data there after stay the same(I have a timer to call the function again and I did put in keep alive)The idea is to send a login once and there after get updated data *Do I need to request all data or can I request only a selected amount for instance Thanks in advance. `namespace WindowsFormsApp7 { public partial class Form1 : Form { private string elementId = "BTC"; private Timer timer1; HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

    public Form1()
    {
        InitializeComponent();
        InitTimer();
    }

    public void InitTimer()
    {   //initiaize and keep a timer
        timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);//run "timer1_Tick" function when timer is up
        timer1.Interval = 60000; // in miliseconds
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (doc.Text != "")
        {
            getNumbers(doc);
        }
    }

    private void btnGo_Click(object sender, EventArgs e)
    {
        //txtOutput.Text = "Working ...";
        var Adress = new Uri(txtUrl.Text);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Adress);
        request.KeepAlive = true;
        //set the cookie container object
        var cookieContainer = new CookieContainer();
        request.CookieContainer = cookieContainer;
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

        //set method POST and content type application/x-www-form-urlencoded
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        //insert your username and password
        string data = string.Format("username={0}&password={1}", txtUserName.Text, txtPassword.Text);
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);

        request.ContentLength = bytes.Length;
        //post username & password
        using (Stream dataStream = request.GetRequestStream())
        {
            dataStream.Write(bytes, 0, bytes.Length);
            dataStream.Close();
        }
        getData(request);
    }

    private void getData(HttpWebRequest request)
    {
        using (WebResponse response = request.GetResponse())
        {
            doc.Load(response.GetResponseStream());
            getNumbers(doc);
        }
    }

    private void getNumbers(HtmlAgilityPack.HtmlDocument doc)
    {
        HtmlNode node = doc.DocumentNode;
        var element = doc.GetElementbyId(elementId);
        string amount = element.InnerText.Replace("&nbsp", " ").Replace(";", "");
        int rindex = amount.IndexOf("R");
        txtRValue.Text = amount.Substring(rindex, (rindex + 12) - rindex);
        debugOutput( amount.Substring(rindex, (rindex + 12) - rindex));
     }

    private void cboCurrency_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (cboCurrency.Text)
        {
            case "ETH":
                elementId = "ETH";
                break;
            default:
                elementId = "BTC";
                break;
        }
       getNumbers(doc);
    }
    #region Debug
    private void debugOutput(string strDebugText)
    {
        try
        {
            System.Diagnostics.Debug.Write(strDebugText + Environment.NewLine);
            txtOutput.Text = txtOutput.Text + strDebugText + Environment.NewLine;
            txtOutput.SelectionStart = txtOutput.TextLength;
            txtOutput.ScrollToCaret();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Write(ex.Message.ToString() + Environment.NewLine);
        }
    }
    #endregion
}

} `

  • The title is a different question than the question body. What exactly do you want to know? – Crowcoder Aug 13 '18 at 12:19
  • @Crowcoder thanks for the fast reply .The idea is to send a login once and then get an update on the data every x minutes. – Cobracom Cobra Aug 13 '18 at 12:37
  • It would probably be easier to use a documented API for this, instead of scraping a web page... For example, coindesk has an API that you can use for free (with some conditions) - https://www.coindesk.com/api/. There are probably other options as well. – jeroenh Aug 13 '18 at 12:38
  • Store the CookieContainer instance and re-use it. Do not create a new one each time if you don't want to login every time. Of course, I don't know what the cookie policy is so you will still have exprirations to deal with. – Crowcoder Aug 13 '18 at 12:39
  • @Crowcoder Thank you your idea work Great used this link for more info https://stackoverflow.com/questions/571964/automatic-cookie-handling-c-net-httpwebrequesthttpwebresponse/572152#572152 – Cobracom Cobra Aug 13 '18 at 13:53

0 Answers0