如何让C#登录网站

时间:2013-02-21 03:22:50

标签: c# python authentication

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

我完全不知道如何在桌面C#应用程序中做这样的事情。有人能指出我正确的方向吗?

由于

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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;
    }
}
相关问题