我有一个非常简单的网站(http://mysite.com),其中包含一个页面,该页面生成随机日期时间值并将其写入标签,该页面的代码是:
<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>
和 C# 代码:
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));
}
}
现在,从 WinForm App 我需要在页面上读取生成的时间,所以对于默认值我这样做:
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;
如何读取需要回发 DropDownList 的 -1 天和 -2 天的生成值(页面内容)。或者这是不可能的?
感谢您的回复!
添加。我无法更改的网站代码!