通过WebRequest C#登录页面

时间:2015-02-03 15:48:50

标签: c# http login webrequest

我正在尝试下载一个我必须在网站上登录的文件。我正在使用this solution,但这不起作用。而不是文件我得到一个下载页面作为请求的结果。在下载页面时有两种不同的形式,一种用于高级会员(我需要),另一种用于免费下载验证码。也许这是问题,存在两个提交按钮。

登录方式:

            CookieContainer container;

            var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            var buffer = Encoding.ASCII.GetBytes(loginData.ToString());
            request.ContentLength = buffer.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();

            container = request.CookieContainer = new CookieContainer();

            var response = request.GetResponse();
            //response.Close();
            CookieContainer = container;
            return response.GetResponseStream();  //this returns a login page

用法:

                var loginAddress = url;
                var loginData = new NameValueCollection
                    {
                      { "frm-downloadDialog-loginForm-username", "username" },
                      { "frm-downloadDialog-loginForm-password", "password" }
                    };


                var client = new CookieAwareWebClient();
                client.Login(loginAddress, loginData);

登录页面:

<input type="text" name="username" class="text" id="frm-downloadDialog-loginForm-username" required="" data-nette-rules="[{&quot;op&quot;:&quot;:filled&quot;,&quot;msg&quot;:&quot;Username required.&quot;}]" value="">
<input type="password" name="password" class="text" id="frm-downloadDialog-loginForm-password" required="" data-nette-rules="[{&quot;op&quot;:&quot;:filled&quot;,&quot;msg&quot;:&quot;Password required.&quot;}]">
<input type="submit" name="login" class="button" id="frm-downloadDialog-loginForm-login" value="Submit">

0 个答案:

没有答案