C# 如何在不使用HttpWebrequest再次登录的情况下更新请求的数据?
我“写”了这段代码,它工作得很好。 它是登录到一个站点并检索数据,这样我就可以看到比特币当前的价格。 *但在我最初登录并检索数据后,它不会更新 之后保持不变(我有一个计时器再次调用函数,然后 这个想法是在更新后发送一次登录到那里 数据 *我是否需要申请所有数据,还是只能申请选定的金额 实例 提前谢谢。 `命名空间WindowsFormsApp7 { 公共部分类Form1:Form { 私有字符串elementId=“BTC”; 私人定时器1; HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument() }C# 如何在不使用HttpWebrequest再次登录的情况下更新请求的数据?,c#,httpwebrequest,bots,C#,Httpwebrequest,Bots,我“写”了这段代码,它工作得很好。 它是登录到一个站点并检索数据,这样我就可以看到比特币当前的价格。 *但在我最初登录并检索数据后,它不会更新 之后保持不变(我有一个计时器再次调用函数,然后 这个想法是在更新后发送一次登录到那里 数据 *我是否需要申请所有数据,还是只能申请选定的金额 实例 提前谢谢。 `命名空间WindowsFormsApp7 { 公共部分类Form1:Form { 私有字符串elementId=“BTC”; 私人定时器1; HtmlAgilityPack.HtmlDocume
`标题与问题正文不同。你到底想知道什么?@Crowcoder感谢你的快速回复。想法是发送一次登录,然后每x分钟更新一次数据。使用有文档记录的API可能比抓取网页更容易……例如,Cointdesk有一个API至少,您可以免费使用(在某些条件下)-。可能还有其他选项。存储CookieContainer实例并重新使用。如果您不想每次登录,请不要每次都创建一个新实例。当然,我不知道cookie策略是什么,因此您仍然需要处理解释。@Crowcoder感谢您的想法。非常好,使用此链接获取更多信息
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(" ", " ").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
}