2010-07-29 68 views

回答

4

像这样的东西应该可以工作......你需要做一个http post请求并发送你的用户名和密码。要保持“登录状态”,您需要一个cookie容器,然后您可以将其重新用于所有其他请求。

编辑:已更新的测试代码。在进行POST登录之前,需要使用初始的GET请求来登录/登录会话cookie。我还添加了一些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”(成功登录后打开的页面)的流... 因为我是一个新的bie,所以我期待完美的运行代码:( – xtremist 2010-07-29 07:00:22

+0

@xtremist:我没有帐户,所以我很难看到问题所在,但是您应该使用Fiddler或类似的工具。使用Fiddler您可以看到原始的HTTP请求,并且您应该使用它来查找使用普通浏览器与上面的代码登录时的区别 – 2010-07-29 08:01:51

+0

@xtremist:Debug.WriteLine输出什么?在那里输出响应文本 – 2010-07-29 08:06:17