使用httpWebRequest post 用户名和密码返回的还是原页面是怎么回事啊

.Net技术 码拜 8年前 (2016-09-24) 1668次浏览
以登录csdn为例

 
                    <input id="username" name="username" tabindex="1" placeholde
r="输入用户名/邮箱/手机号" class="user-name" type="text" value=""/>
                    <input id="password" name="password" tabindex="2" placeholde
r="输入密码" class="pass-word" type="password" value="" autocomplete="off"/>
string strId = "client";
                string strPassword = "123456";
                ASCIIEncoding encoding = new ASCIIEncoding();
                string postData = "username=" + strId;
                postData += ("&password=" + strPassword);
                byte[] data = encoding.GetBytes(postData);
                // Prepare web request...  
                HttpWebRequest myRequest =
                (HttpWebRequest)WebRequest.Create("https://passport.csdn.net");
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                // Send the data.  
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                // Get response  
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string content = reader.ReadToEnd();
                Console.WriteLine(content);

返回的还是登录页面的html,怎么办啊

解决方案

20

访问 https://passport.csdn.net 有这样的 cookie
Set-Cookie: JSESSIONID=B4E1ED1CCD5CE0CC8F78292F93E9F0EE.tomcat1; Path=/; HttpOnly
Set-Cookie: JSESSIONID=B4E1ED1CCD5CE0CC8F78292F93E9F0EE.tomcat1
Set-Cookie: LSSC=LSSC-19863-sHcPqxgO10UZpkxcgupST6bzUsU1Ie-passport.csdn.net; Expires=Sun, 16-Oct-2016 09:08:30 GMT; Path=/; Secure
但你并没有回送回去
没有了 sesson,他怎么知道登录的是您呢?

20

另外,登录表单中还有这么一段

<!-- 该参数可以理解成每个需要登录的用户都有一个流水号。只有有了webflow发放的有效的流水号,用户才可以说明是已经进入了webflow流程。否则,没有流水号的情况下,webflow会认为用户还没有进入webflow流程,从而会重新进入一次webflow流程,从而会重新出现登录界面。 -->
<input type="hidden" name="lt" value="LT-192203-LFeKbmm6yS3OFv2RKBCpdBKrTvfD3p" />
<input type="hidden" name="execution" value="e1s1" /> 
<input type="hidden" name="_eventId" value="submit" />

你似乎也没有看到
至少你需要先访问表单页,解析出动态产生的 lt 的值
然后才有可能提交成功


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明使用httpWebRequest post 用户名和密码返回的还是原页面是怎么回事啊
喜欢 (0)
[1034331897@qq.com]
分享 (0)