私は、ある特定の Web サイトで投票プロセスの自動化に取り組んでいます。私のプログラムは次のことを行う必要があります: Web サイトにログインし、特定のミュージシャンに投票し、ログアウトします。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();
}
}
私は古いクッキーを取り除こうとし、keep-aliveとAllowAutoRedirectパラメーターとContent-typeの異なる値を試しました。私には何もうまくいかないようです。アプリケーションは要求に応じてハングし、タイムアウトにより失敗します。
何が問題なのか知っていますか?