如何进行C#登录网站

如何进行C#登录网站,c#,python,authentication,C#,Python,Authentication,我正试图弄清楚如何让我的C#应用程序登录到我的网站应用程序。我完全不知道从哪里开始。通常在网站上,用户只需输入用户和密码,检查或不检查“记住我”按钮,然后单击“登录”。我的python框架验证登录,然后在响应的头中设置cookie以保存登录cookie。当用户尝试访问某个页面时,它会检查是否可以找到cookie,如果可以,则会让用户保持登录状态 string string loginData = "username=***&passowrd=***&next=/hurt/";

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

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);
我完全不知道如何在桌面C#应用程序中进行类似的操作。有人能给我指出正确的方向吗

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);
谢谢

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);
我正试图弄清楚如何让我的桌面C#应用程序登录到我的 网站应用

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);
使用 班级

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);

如果要模拟浏览器,请使用COM。下面的例子

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);
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;
    }
}

我将如何处理响应内容?我如何处理保持响应中返回的cookie,以便用户登录,直到应用程序关闭为止?我是否需要来回发送一些东西,说这个用户就是登录的用户?如果你想象它是一个不可见的web浏览器,一旦它登录到你的
python框架验证登录,然后在响应的标题中设置一个cookie以保存登录cookie。
这样,您第二次登录时就不需要再次登录。那么WebClient是否像普通web浏览器一样管理每个请求的所有cookie等?那么,在下一个请求中,它会传递从第一个响应接收到的cookie吗?