新人报到,多多关照!我试图用HttpHelper模拟登陆网站I V O L A T I L I T Y . C O M. 此站需要用户名和密码登陆,登陆后由/login.j转发到/home.j才能开始查询。我用chrome的开发工具截获其Http通讯的相关信息如下:
登录页header:
登录后转home的header:
查询页header:
退出页header:
该站允许一个用户实例同时登录,但在测试中,需要反复登陆,所以,我登录前先模拟退出。用于不知道如何检验用户是否已登录,我只好发送一个GET的请求来退出:
[C#] 纯文本查看 复制代码 HttpHelper http = new HttpHelper();
HttpItem item = null;
string cookie = "";
item = new HttpItem()
{
URL = "https://www.i v o l a t i l i t y.com/logoff.j?logoff=1&cookies_disabled=true",
Method = "get",
Cookie = null,
ICredentials = null,
};
HttpResult html = http.GetHtml(item);
接着登录:
[C#] 纯文本查看 复制代码 item = new HttpItem()
{
URL = "https://www.i v o l a t i l i t y.com/login.j",
Encoding = null,
Method = "Post",
ContentType = "application/x-www-form-urlencoded",
Postdata = "username=ABCDE&password=123456789" +
"&login__is__sent=1&step=1",
KeepAlive = true,
Referer = "https://www.i v o l a t i l i t y.com/login.j",
};
html = http.GetHtml(item);
if (html.Header["set-cookie"] != null)
cookie = html.Header["set-cookie"];
开始查询:
[C#] 纯文本查看 复制代码 string [] stocks = {"IBM", "AAPL"};
if (html.StatusCode == System.Net.HttpStatusCode.OK)
{
Console.WriteLine("Logined");
string mysql = "insert into Options ";
foreach (string s in stocks)
{
url = "http://www.i v o l a t i l i t y.com/options.j?ticker={0}";
string gourl = string.Format(url, s);
item = new HttpItem()
{
URL = gourl,
Method = "get",
Cookie = cookie,
};
HttpResult res = http.GetHtml(item);
// 判断是否被拒
if (!res.Html.Contains("We are sorry"))
Console.WriteLine("Found the SORRY page\n");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(res.Html);
string sql = mysql;
HtmlNodeCollection cells = doc.DocumentNode.SelectNodes("//font[@class='s2']");
for (int k = 0; k < cells.Count; k++)
{
HtmlNode node = cells[k];
if (node.InnerText.Contains("Index mean"))
{
sql += "(symbol, Current, OneWkAgo, OneMthAgo, YearHi, YearLo) values (" + s;
sql += ", " + (cells[k + 1].InnerText) + ", " +
(cells[k + 2].InnerText) + ", " +
(cells[k + 3].InnerText) + ", " +
(cells[k + 4].InnerText) + ", " +
(cells[k + 5].InnerText) + ")";
break;
}
k++;
}
Execute(sql);
Console.WriteLine(sql);
Thread.Sleep(2000);
}
}
大概就是这样,运行后发现,登录成功后第一次查询是没问题的,能拿到数据,但第二次用同样的cookie(即使是同样的股票代码),却被拒了。请飞哥和各位帮忙看看哪里出错了!
|