0

I have very simple website (http://mysite.com), that contains one page, that page generates random datetime value and writes it to lable, code of that page is:

<form id="form1" runat="server">
    <div>
        <asp:Label ID="lblGeneratedDateTime" runat="server"></asp:Label>
        <br />
        <asp:DropDownList ID="ddlGenerateDateTime" runat="server" AutoPostBack="true" 
            onselectedindexchanged="ddlGenerateDateTime_SelectedIndexChanged">
            <asp:ListItem Selected="True" Value="0">Today</asp:ListItem>
            <asp:ListItem Value="1">-1 day</asp:ListItem>
            <asp:ListItem Value="2">-2 days</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>

and C# code:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblGeneratedDateTime.Text = GenerateDateTime(0).ToString();
        }
    }
    protected void ddlGenerateDateTime_SelectedIndexChanged(object sender, EventArgs e)
    {
        int value = Convert.ToInt32(ddlGenerateDateTime.SelectedValue);
        lblGeneratedDateTime.Text = GenerateDateTime(value).ToString();
    }

    Random random = new Random();

    private DateTime GenerateDateTime(int minusDays)
    {
        DateTime date = DateTime.Now.AddDays(-minusDays);

        return new DateTime(date.Year, date.Month, date.Day,
            random.Next(0, 23), random.Next(0, 59), random.Next(0, 59));
    }

}

Now, from WinForm App I need READ generated time on the page, so for default value I do:

string url = "http://mysite.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
string page = streamReader.ReadToEnd();

int start = str.IndexOf("lblGeneratedDateTime\">") + 22;
int end = str.LastIndexOf("</span>");

string generatedDateTime = str.Substring(start, end - start);

return generatedDateTime;

How can I read generated values (page content) for -1 and -2 days which needs do postback of DropDownList. Or it's impossible?

Thanks for any reply!

Added. Code of the site I can't change!

ihorko
  • 6,855
  • 25
  • 77
  • 116
  • the HTML agility pack will probably do what you need http://htmlagilitypack.codeplex.com/ – Liam Jun 26 '13 at 13:55
  • Here's a *How to...* http://stackoverflow.com/questions/846994/how-to-use-html-agility-pack – Liam Jun 26 '13 at 13:56
  • any code examples how to change (or read content) of DropDown values ? – ihorko Jun 26 '13 at 13:58
  • I've not used it extensively but there is plenty of documentation on the web. It will turn your html into an object structure you can query without using string matching. Or I do agree with Richard, if you can, why not return something you can parse easier – Liam Jun 26 '13 at 14:00
  • 1
    I can't change code of the site – ihorko Jun 26 '13 at 14:01

2 Answers2

1

You can't do this like that... you download the html response as a string, so it's impossible to interact with the page and simulate a dropdown change..

You can try using WebClient and submit the form page with different value for you dropdown.. this will generate different responses that you can parse with your code above.

LoSTxMiND
  • 332
  • 3
  • 8
0

It needs to use WebBrowser instead.

WebBrowser wb = new WebBrowser();

wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);

string url = "http://mysite.com";

wb.Navigate(url);

HtmlElement item = wb.Document.GetElementById("ddlGenerateDateTime");

then

item.SetAttribute("value", "1");
item.InvokeMember("onchange");

item.SetAttribute("value", "2");
item.InvokeMember("onchange");

in that case it's possible to send postback to page.

ihorko
  • 6,855
  • 25
  • 77
  • 116