2013-05-06 2 views
0

특정 웹 사이트에서 투표 프로세스의 자동화 작업을하고 있습니다. 내 프로그램에서 다음 작업을 수행해야합니다. 웹 사이트에 로그인하고 특정 뮤지션에게 투표 한 다음 로그 아웃하십시오. 나는 모든 요청을 수행하기 위해 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(); 

      } 
     } 

: 여기 Log out request in FireBug Log out request with details

내 코드입니다 쿠키를 시도하고 다른 값을 연결 유지AllowAutoRedirect 매개 변수와 내용 유형. 아무것도 나를 위해 일하는 것 같지 않습니다. 응용 프로그램이 요청에 따라 멈추고 시간 초과로 실패합니다.

아시다시피, 무엇이 문제입니까?

+0

어떤 행이 정지합니까? – FlyingStreudel

+0

'HttpClient '란 무엇입니까? 확실히 System.Net.Http.HttpClient (웹 API라고도 함)가 아닙니다. –

+0

계속됩니다. var logOutResponse = logOutReq.GetResponse(); 약 3 분 동안 WebException 및 "작업 시간 초과"메시지와 함께 실패합니다. HttpClient는 HttpWebRequest/HttpWebResponse를 둘러싼 "래퍼"입니다. 문제가되지 않습니다. –

답변

0

문제가 해결되었습니다. 나는 각 요청에 다음 매개 변수를 추가 한 :

request.KeepAlive = true; 

은 또한, 나는 널 (null)에 로그 아웃 요청의 내용 유형을 설정 한 :

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

이제 모든 것이 잘 작동합니다.