0

私は、ある特定の Web サイトで投票プロセスの自動化に取り組んでいます。私のプログラムは次のことを行う必要があります: Web サイトにログインし、特定のミュージシャンに投票し、ログアウトします。HttpWebRequest クラスを使用してすべてのリクエストを実行します。承認と投票は成功したのですが、正常にログアウトできません。単純な GET 要求で、コンソール アプリケーションがしばらくハングし、その後、応答タイムアウト例外によってクラッシュします。

だから、ここに私が持っているものがあります - ブラウザでエミュレートされたログアウトリクエスト:

FireBug でのログアウト要求 詳細を含むログアウト要求

これが私のコードです:

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-aliveAllowAutoRedirectパラメーターとContent-typeの異なる値を試しました。私には何もうまくいかないようです。アプリケーションは要求に応じてハングし、タイムアウトにより失敗します。

何が問題なのか知っていますか?

4

1 に答える 1

0

問題を解決しました。各リクエストに次のパラメーターを追加しました。

request.KeepAlive = true;

さらに、ログアウト リクエストの Content-type を null に設定しました。

connectionService.ContentType = null;
var logOutResponse = connectionService.DoGet(logOutUri, cookies);

今、すべてが正常に動作します。

于 2013-05-08T18:36:53.940 に答える