public void alimamapost() { CookieContainer cc = new CookieContainer(); ASCIIEncoding encoding = new ASCIIEncoding(); string postData = "action=MembersAction&event_submit_do_login=true&forward=&query_string=&[email protected]&originalLogpasswd=a8824766&_fmm.l._0.lo=c781f1ee5ab17dff12ff6019325688f2&dologin=";
byte[] data = Encoding.GetEncoding("GB2312").GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.alimama.com/membersvc/member/login.htm"); myRequest.CookieContainer = cc; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"; myRequest.Referer = "http://www.alimama.com/membersvc/member/login.htm";
{ System.Windows.Forms.HtmlDocument document =this.webBrowser1.Document;
if ( document == null )
{
return;
} document.All["logname"].SetAttribute( "Value", "你的用户名" ); //用户名
document.All["originalLogpasswd"].SetAttribute( "Value", "你的密码" ); //密码
document.All["dologin"].InvokeMember( "click" ); }
VS2008
我需要用httpwebrequest/httpwebresponse来处理,不能用webbrowser.
账号:[email protected]
密码:a8824766
谢谢大家
我只需要使用帐号+密码能成功登录就可以,现在主要是它提交数据比较复杂,它不知是直接提交表单,它是把相关信息写入到COOKIE,还有一部分写入表单,然后提交,我现在就是COOKIE这块没有处理好.
现在按照它的顺序,是需要将logname,password,isRemember都写入cookie,然后提交上去.但是仍未返回登录成功的信息
可否说一下你是如何处理登陆的?
{ CookieContainer cc = new CookieContainer();
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "action=MembersAction&event_submit_do_login=true&forward=&query_string=&[email protected]&originalLogpasswd=a8824766&_fmm.l._0.lo=c781f1ee5ab17dff12ff6019325688f2&dologin=";
byte[] data = Encoding.GetEncoding("GB2312").GetBytes(postData); HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://www.alimama.com/membersvc/member/login.htm"); myRequest.CookieContainer = cc;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
myRequest.Referer = "http://www.alimama.com/membersvc/member/login.htm";
myRequest.AllowAutoRedirect = true;
myRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*";
myRequest.ContentLength = data.Length;
myRequest.Timeout = 10000;
myRequest.Method = "POST";
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close(); HttpWebResponse oresponse; try
{
oresponse = (HttpWebResponse)myRequest.GetResponse();
}
catch (WebException ex)
{
if (ex.Response != null)
{
oresponse = (HttpWebResponse)ex.Response;
}
else
{
throw ex;
}
} Stream dataStream = oresponse.GetResponseStream(); try
{
StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312")); string responseFromServer = reader.ReadToEnd(); reader.Close();
dataStream.Close();
oresponse.Close();
}
catch (Exception k1)
{
throw k1;
} myRequest =
(HttpWebRequest)WebRequest.Create("http://www.alimama.com/membersvc/adzone/site.htm?addsite=true");
myRequest.CookieContainer = cc;
try
{
oresponse = (HttpWebResponse)myRequest.GetResponse();
}
catch (WebException ex)
{
if (ex.Response != null)
{
oresponse = (HttpWebResponse)ex.Response;
}
else
{
throw ex;
}
} dataStream = oresponse.GetResponseStream(); try
{
StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312")); string responseFromServer = reader.ReadToEnd(); reader.Close();
dataStream.Close();
oresponse.Close(); }
catch (Exception k1)
{
throw k1;
}
}
我测试成功了,跳转发广告页面是对的,你试试
成功后应该跳转到http://www.alimama.com/membersvc/index.html
并且显示当前登录用户信息to dlppeng:
我用人家监视了,每次的COOKIE都是不同的,服务器端做了处理
代码没在这台机器上,否则我就贴出来给你瞧瞧
然后按照监视的情况 首先获取初始页生成的COOKIE,保存起来,在提交登陆前好象还有一个页面生成了cookie
你也得获取 然后把最终提交登陆时所需的COOKIE一并提交注意要考虑 清除IE cookie 后的情况 这网站没有asp.NetSessionID 建议使用webbrowser和httpwebrequest 结合的方式 用webbrowser登陆,之后的操作用httpwebrequest处理, 登陆后的COOKIE直接从document中获取(在文挡完成事件里面写),即便是cookie 有变化 也不需要httpwebrequest 再去找
to 29楼:
成功后应该跳转到http://www.alimama.com/membersvc/index.html
并且显示当前登录用户信息 你调试了没?第一步成功后跳转的就是http://www.alimama.com/membersvc/index.html,你把得到的html在dreamwaver里面看看就知道了,显示用户信息的是另外一个链接,你做点处理就是了