문제

I am working on automation of voting process on one particular web site. My program needs to do the following things: log in to the web site, vote for particular musician, then log out. I use HttpWebRequest class to perform all the requests. I succeeded with authorization and voting, but I cannot log out normally. My simple GET request makes my console application hang for a while and after that it crashes by response time out exception.

So, here is what I have - the logout request emulated in browser:

Log out request in FireBug Log out request with details

Here is my code:

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();
                   
            }
        }

I have tried to get rid of old cookies and tried different values of keep-alive and AllowAutoRedirect parameters and Content-type. Seems nothing works for me. The application hangs on request and fails by time out.

Do you know, what is the problem?

도움이 되었습니까?

해결책

I have resolved the problem. I have added the following parameter to each request:

request.KeepAlive = true;

Moreover, I have set the Content-type of log out request to null:

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

Now everything works fine.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top