2010-07-29 2 views

답변

4

다음과 같은 것이 작동해야합니다 ... http 게시 요청을하고 사용자 이름과 암호를 보내야합니다. "로그인"상태를 유지하려면 다른 모든 요청에 ​​대해 재사용 할 수있는 쿠키 컨테이너가 필요합니다.

편집 : 테스트 된 코드가 업데이트되었습니다. POST 로그인을하기 전에 세션 쿠키를 갖기 위해/login에 대한 초기 GET 요청이 필요했습니다. 또한 HTTP 헤더를 추가하여 브라우저의 "일반적인"요청과 동일하게 만들었습니다.

void Main() 
{ 
    //We need a container to store the cookies in. 
    CookieContainer cookies = new CookieContainer(); 

    //Request login page to get a session cookie 
    GETHtml("http://www.purevolume.com/login", cookies); 

    //Now we can do login 
    Login("some-user-name", "some-password", cookies); 
} 

public bool Login(string Username, string Password, CookieContainer cookies) 
{ 
    string poststring = string.Format("username={0}&password={1}&user_login_button.x=63&user_login_button.y=13&user_login_button=login", 
           Username, Password); 
    byte[] postdata = Encoding.UTF8.GetBytes(poststring); 

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.purevolume.com/login"); 
    webRequest.CookieContainer = cookies; 
    webRequest.Method = "POST"; 
    webRequest.Referer = "http://www.purevolume.com/login"; 
    webRequest.Headers.Add("origin", "http://www.purevolume.com"); 
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;"; 
    webRequest.ContentType = "application/x-www-form-urlencoded"; 
    webRequest.ContentLength = postdata.Length; 
    using (Stream writer = webRequest.GetRequestStream()) 
     writer.Write(postdata, 0, postdata.Length); 

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
    { 
     //We need to add any response cookies to our cookie container 
     cookies.Add(webResponse.Cookies); 

     //Only for debug 
     using (var stream = new StreamReader(webResponse.GetResponseStream())) 
      System.Diagnostics.Debug.WriteLine(stream.ReadToEnd()); 

     return (webResponse.StatusCode == HttpStatusCode.OK); 
    } 
} 

public string GETHtml(string url, CookieContainer cookies) 
{ 
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
    webRequest.CookieContainer = cookies; 
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;"; 
    webRequest.Referer = "http://www.purevolume.com/login"; 

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
    {  
     //We need to add any response cookies to our cookie container   
     cookies.Add(webResponse.Cookies); 

     using (var stream = new StreamReader(webResponse.GetResponseStream())) 
      return stream.ReadToEnd(); 
    } 
} 
+0

난이 시도하지만 페이지가 여전히 로그인 페이지에 남아에 statusCode이 참 ... 어떻게 제공 사용자 이름과 암호가 정확한지 알 수 있을까? 나는 응답이 "http://www.purevolume.com/dashboard"(성공한 로그인 이후에 열리는 페이지)의 스트림을 돌려줘야한다고 생각한다 ... 나는 완벽한 실행 코드를 기대하고 있기 때문에 나는 새로운 새이다. ( – xtremist

+0

@xtremist : 계정이 없으므로 문제가 무엇인지 알기가 어렵지만 Fiddler와 같은 도구를 사용해야합니다. Fiddler를 사용하면 원시 HTTP 요청을 볼 수 있으며이 도구를 사용하여 일반 브라우저로 로그인 할 때의 차이점 위의 코드와 비교했을 때 –

+0

@xtremist : Debug.WriteLine 출력은 무엇입니까? 응답 텍스트가 출력됩니다 –

관련 문제