c# 模拟登录帐号登录网站得不到应该有的数据
c#模拟登录知乎:const string Url = @"http://www.zhihu.com/login";
处理的大概流程也知道,就是用程序模拟浏览器,发送request,然后得到response,继而得到相应的html数据进行分析。
网上看了好多,用起来感觉不是很适应,大多都是应用cookies获取,我想先不用太复杂,直接帐号密码登录获取页面就行了,最后获取的html保存为html后如下所示:

可实际上我想得到最终的html是这样的

我的代码
处理的大概流程也知道,就是用程序模拟浏览器,发送request,然后得到response,继而得到相应的html数据进行分析。
网上看了好多,用起来感觉不是很适应,大多都是应用cookies获取,我想先不用太复杂,直接帐号密码登录获取页面就行了,最后获取的html保存为html后如下所示:

可实际上我想得到最终的html是这样的

我的代码
public static string SendRequest()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//CookieContainer cc = new CookieContainer();
//---simulate account:
request.Method = "POST";
//request.ContentType = "";
//request.CookieContainer = cc;
string data = "_xsrf:aaaanumber&" +
"email:my@mail.com&" +
"password:password&" +
"rememberme:y";
data = HttpUtility.UrlEncode(data);
byte[] postdata = Encoding.UTF8.GetBytes(data);
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentLength = postdata.Length;
request.AllowAutoRedirect = false;
request.KeepAlive = true;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postdata, 0, postdata.Length);
stream.Close();
}
HttpWebResponse res = (HttpWebResponse)request.GetResponse();
//res.Cookies = cc.GetCookies(request.RequestUri);
// return response;
string htmlData = string.Empty;
Stream datastream = res.GetResponseStream();
if (datastream != null)
{
var reader = new StreamReader(datastream);
htmlData = reader.ReadToEnd();
reader.Close();
datastream.Close();
}
res.Close();
return htmlData;
}
...全文
相关推荐
C#实现微信公众帐号主动发消息给用户
asp.net模拟微信公众帐号登录,实现主动发送信息给用户,亲自测试过可以使用. asp.net模拟微信公众帐号登录,实现主动发送信息给用户,亲自测试过可以使用.
.NET C#使用微信公众号登录网站
适用于:本文适用于有一定微信开发基础的用户 引言:花了300大洋申请了微信公众平台后,发现不能使用微信公众号登录网站(非微信打开)获得微信帐号。仔细研究后才发现还要再花300大洋申请微信开放平台才能
asp.net 帐号只能在一处登录
当提到"asp.net 帐号只能在一处登录"时,这通常涉及到单点登录(Single Sign-On, SSO)的概念。
.net实现微信公众帐号主动发消息给用户
asp.net模拟微信公众帐号登录,实现主动发送信息给用户
c#连接oracle实现用户登录以及密码的修改
在C#中,连接Oracle数据库并实现用户登录验证及密码修改功能是常见的需求,尤其是在开发企业级应用时。本文将详细讲解如何通过C#代码来完成这个任务。


