我正在研究一个特定网站上的投票过程自动化。我的程序需要做以下事情:登录网站,为特定的音乐家投票,然后退出。我使用 HttpWebRequest 类来执行所有请求。我授权和投票成功,但我无法正常注销。我的简单 GET 请求使我的控制台应用程序挂起一段时间,然后它因响应超时异常而崩溃。
所以,这就是我所拥有的 - 在浏览器中模拟的注销请求:
这是我的代码:
private static void Vote(string email, string password)
{
// log in
HttpClient connectionService = new HttpClient();
connectionService.ContentType = HttpClient.REQUEST_FORM_URLENCODED_TYPE;
CookieContainer cookies = new CookieContainer();
string logInParams = String.Format("uname={0}&upass={1}&l=ru", email, password);
Uri logInRequestUri = new Uri(loginHost);
var logInResponse = connectionService.DoPost(logInRequestUri, logInParams, cookies);
// take cookies and pass them to the next request
if (logInResponse.StatusCode == HttpStatusCode.OK)
{
// vote
string voteParams = String.Format("nid={0}&l=ru", voteID);
Uri voteRequestUri = new Uri(voteHost);
var voteResponse = connectionService.DoPost(voteRequestUri, voteParams, cookies);
// Until this moment everything works fine
// log out
Uri logOutUri = new Uri(logoutHost);
// The commented part below does did not work. I replaced it with a
// "fresh" request, created from scratch
//connectionService.ContentType = "text/html";
//var logOutResponse = connectionService.DoGet(logOutUri, cookies);
var logOutReq = (HttpWebRequest) WebRequest.Create(logOutUri);
logOutReq.KeepAlive = true;
logOutReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
logOutReq.Host = "thebestcity.ua";
logOutReq.Referer = "http://thebestcity.ua/competition/";
logOutReq.Method = WebRequestMethods.Http.Get;
logOutReq.CookieContainer = cookies;
var logOutResponse = logOutReq.GetResponse();
}
}
我试图摆脱旧的 cookie,并尝试了keep-alive和AllowAutoRedirect参数和Content-type的不同值。似乎没有什么对我有用。应用程序按请求挂起并因超时而失败。
你知道,有什么问题吗?