简体   繁体   English

使用C#登录到第三方网站

[英]login to third party website using c#

I have URL 我有网址

and by viewing its source code i found the form used for login in this URL 通过查看其源代码,我找到了用于此URL登录的表单

<form name="frmLogin" action="j_spring_security_check" method="post"><!-- FORM START -->
                <div class="pod">
                    <div class="single_input">
                        <label class="label" for="j_username">User Name</label>
                        <div class="field">
                            <input type="text" id="j_username" name="j_username" value="">
                        </div>
                    </div>
                    <div class="single_input">
                        <label class="label" for="j_password">Password</label>
                        <div class="field">
                            <input type="password" id="j_password" name="j_password" value="">
                        </div>
                    </div>
                    <div class="single_input">

                        <div class="field">
                            <a href="externalResetPassword">Forgot Password</a>
                        </div>
                    </div>
                </div>
                <div class="cta center">
                    <input name="btnContinue" type="submit" value="Login" class="btnContinue">
                </div>
            </form>

My question is, How can i check the user name and password i entered it correct without using browser. 我的问题是,如何在不使用浏览器的情况下正确检查输入的用户名和密码。 I hope i can do that using c# HttpWebRequest 我希望我可以使用C#HttpWebRequest做到这一点

Edit: I found some code that seems to be usefull but i am not understanding it properly 编辑:我发现一些似乎有用的代码,但我不能正确理解

here is the code 这是代码

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://www.fastactportal.com/login");
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
            req.Method = "POST";
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            req.Headers.Add("Accept-Language: en-us,en;q=0.5");
            req.Headers.Add("Accept-Encoding: gzip,deflate");
            req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            req.KeepAlive = true;
            req.Headers.Add("Keep-Alive: 300");
            req.Referer = "https://www.fastactportal.com/login";

            req.ContentType = "application/x-www-form-urlencoded";

            String Username = "j_username";
            String PassWord = "j_password";

            StreamWriter sw = new StreamWriter(req.GetRequestStream());
            sw.Write("application=portal&url=http%3A%2F%2Fwww.fastactportal.com%2Fportal%2Fredirect%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Ftype%3Dssologin%26url%3D%2Fportal%2Fshow%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Fidc%3D1023278&realm=sso&j_username=" + Username + "&j_password=" + PassWord + "&x=16&y=11");
            sw.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();


            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();

            foreach (Cookie cook in response.Cookies)
            {
                tmp += "\n" + cook.Name + ": " + cook.Value;
            }


            Response.Write(tmp);
            Response.End();

this line very confusing 这条线很混乱

sw.Write("application=portal&url=http%3A%2F%2Fwww.fastactportal.com%2Fportal%2Fredirect%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Ftype%3Dssologin%26url%3D%2Fportal%2Fshow%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Fidc%3D1023278&realm=sso&j_username=" + Username + "&j_password=" + PassWord + "&x=16&y=11");

Can anybody help me to understand this 有人可以帮我理解吗

Many web applications such as proxy web servers and multiple search engines are required to access the HTML pages of other websites. 访问其他网站的HTML页面需要许多Web应用程序,例如代理Web服务器和多个搜索引擎。 The classes WebClient , WebRequest , and WebResponse are usually used to perform these requirements in ASP.NET. WebClientWebRequestWebResponse类通常用于在ASP.NET中执行这些要求。

On the other hand, a WebBrowser control is used in a Windows Forms application to browse web pages and other browser-enabled documents. 另一方面,Windows窗体应用程序中使用WebBrowser控件来浏览网页和其他启用浏览器的文档。 WebBrowser provides many events to track data processes, and many properties and methods to access and create new contents on the HTML element level. WebBrowser提供了许多事件来跟踪数据过程,并提供许多属性和方法来访问和创建HTML元素级别的新内容。

In the bellow link i found the website which is using the window browser control 在下面的链接中,我找到了使用窗口浏览器控件的网站

http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM