2013-02-21 79 views
0

我想弄清楚如何使我的C#应用​​程序登录到我的网站应用程序。我完全不知道从哪里开始。通常在网站上,用户只需输入用户名和密码,然后选中或不检查记住我的按钮并单击登录。我的python框架验证登录,然后在响应的头部设置一个cookie来保存登录cookie。当用户尝试访问一个页面时,它会检查它是否可以找到该cookie,如果它确实可以让用户登录。如何使C#登录到网站

我绝对没有想法,我会如何去做这样的事情桌面C#应用程序。有人能指引我朝着正确的方向吗?

感谢

回答

1

我试图找出如何使我的桌面C#应用程序登录我的网站 应用

使用WebClient 类。

string string loginData = "username=***&passowrd=***&next=/hurt/"; 

WebClient wc = new WebClient(); 

wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5"); 
wc.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
wc.Headers.Add("Accept-Encoding", "identity"); 
wc.Headers.Add("Accept-Language", "en-US,en;q=0.8"); 
wc.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3"); 
wc.Headers.Add("ContentType", "application/x-www-form-urlencoded"); 
string response = wc.UploadString("http://xyz.com/accounts/login/", "POST", loginData); 
+0

我该如何处理响应内容?我如何处理保持响应中返回的cookie,以便用户登录,直到应用程序关闭为止?我是否需要来回发送某种东西,说这个用户是登录的用户? – MasterGberry 2013-02-21 03:29:36

+0

如果你想象它是一个无形的web浏览器,一旦它登录你的'python框架验证登录,然后在响应的头部设置一个cookie来保存登录cookie.'所以你第二次去http: //xyz.com它不应该要求另一个登录。 – 2013-02-21 03:43:06

+0

那么WebClient如何管理每个请求中的所有cookie等,就像普通的Web浏览器一样?那么下一个请求会传递从第一个响应中收到的cookie? – MasterGberry 2013-02-21 03:44:11

0

如果你想模拟一个浏览器,然后使用COM。下面的示例

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

// Add a reference to "C:\Windows\System32\shdocvw.dll" 

namespace BrowserAutomation 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var ie = new SHDocVw.InternetExplorer(); 
      // Ensure that ie.Quit() is called at the end via the destructor 
      var raii = new IE_RAII(ie); 
      // Just so you can see what's going on for now 
      ie.Visible = true; 
      ie.Navigate2("http://www.wellsfargo.com"); 
      var document = GetDocument(ie); 
      var userid = document.getElementById("userid"); 
      userid.Value = "billy.everyteen"; 
      var password = document.getElementById("password"); 
      password.Value = "monroebot"; 
      var form = document.Forms("signon"); 
      form.Submit(); 
      // Hang out for a while... 
      System.Threading.Thread.Sleep(10000); 
     } 
     // Poor man's check and wait until DOM is ready 
     static dynamic GetDocument(SHDocVw.InternetExplorer ie) 
     { 
      while (true) 
      { 
       try 
       { 
        return ie.Document; 
       } 
       catch (System.Runtime.InteropServices.COMException e) 
       { 
        if (e.Message != "Error HRESULT E_FAIL has been returned " + 
            "from a call to a COM component.") 
        { 
         throw e; 
        } 
       } 
       System.Threading.Thread.Sleep(1000); 
      } 
     } 
    } 
    class IE_RAII 
    { 
     public IE_RAII(SHDocVw.InternetExplorer ie) 
     { 
      _ie = ie; 
     } 
     ~IE_RAII() 
     { 
      _ie.Quit(); 
     } 
     private SHDocVw.InternetExplorer _ie; 
    } 
}