C# 模拟登录后,怎样打开登录后的网页页面? 如题,C#模拟登录网页(具体是博客),登录成功后(代码实现,自己已经把登录的代码写出来了),怎样打开登录后的页面?需要调用哪个函数(本人新手),csdn的热心朋友,帮帮忙!谢谢!有代码就贴一下吧! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 记录下Cookei打开别的页面的时候把你登录后的Cookie发送过去。 两钻大哥,好激动啊!我的代码如下:public class HttpHelper { public static CookieContainer Cookies = new CookieContainer(); public static string GetHttpResponse(string url, string postdata) { try { HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url); MyRequest.Method = "POST"; MyRequest.ContentLength = postdata.Length; MyRequest.CookieContainer = Cookies; MyRequest.KeepAlive = true; MyRequest.AllowAutoRedirect = true; MyRequest.ContentType = "application/x-www-form-urlencoded"; MyRequest.UserAgent = " Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1"; MyRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; if (postdata != null) { ASCIIEncoding MyEncoding = new ASCIIEncoding(); byte[] MyByte = MyEncoding.GetBytes(postdata); Stream MyStream = MyRequest.GetRequestStream(); MyStream.Write(MyByte, 0, postdata.Length); MyStream.Close(); } string getContent = null; Encoding myEncoding = Encoding.GetEncoding("UTF-8"); HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse(); if (MyResponse.StatusCode == HttpStatusCode.OK) { /*获取响应页面cookie*/ // MyResponse.Cookies = MyRequest.CookieContainer.GetCookies(MyRequest.RequestUri); //CookieCollection cook = MyResponse.Cookies; //string strCook = MyRequest.CookieContainer.GetCookieHeader(MyRequest.RequestUri); //MessageBox.Show("adfdfasdf"); Stream MyNewStream = MyResponse.GetResponseStream(); StreamReader MyStreamReader = new StreamReader(MyNewStream, myEncoding); getContent = MyStreamReader.ReadToEnd(); MyStreamReader.Close(); } MyResponse.Close(); return getContent; } catch (Exception) { return string.Empty; } }请问:登录后的cookie是保存在public static CookieContainer Cookies = new CookieContainer();中的Cookie中吗?这个Cookie是不是就是你说的做为访问登录页面需要的cookie呢?真的很希望得到你的帮助和回答,有范例代码就贴一贴吧!呵呵,再次感谢你!我是新手,上面的代码也不是很理解,这知道大概个原理和过程! 正是这个 CookieContainer。下面的登录3dezu网站并留言的代码,供参考:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.Text.RegularExpressions;using System.Web;using System.IO;using System.IO.Compression;using System.Threading;namespace login3Dezu{ public partial class Form1 : Form { CookieContainer cc = new CookieContainer(); string formhash = null; BackgroundWorker bw = new BackgroundWorker(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); bw.RunWorkerAsync(); } void bw_DoWork(object sender, DoWorkEventArgs e) { while (!bw.CancellationPending) { StringBuilder sb = new StringBuilder(); Encoding code = Encoding.ASCII; bw.ReportProgress(1,"当前日期:"+DateTime.Now.ToString()+"\r\n\r\n"); // 登录页面 bw.ReportProgress(1, "登录页面\r\n"); string p = "fastloginfield=username&username=XXXXX&password=XXXXX&quickforward=yes&handlekey=ls"; byte[] postData = code.GetBytes(p); string s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1", cc, postData)); // 查看积分 bw.ReportProgress(1, "查看积分\r\n"); s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/home.php?mod=spacecp&ac=credit&op=base", cc, null)); MatchCollection mc = Regex.Matches(s, @"<li[^<]*><em>(?<i>[^<]+)</em>(?<m>\d+)"); foreach (Match m in mc) { sb.AppendLine(string.Format("{0,20} {1}", m.Groups["i"].Value, m.Groups["m"].Value)); } bw.ReportProgress(1,sb.ToString()+"\r\n\r\n"); // 获取 formhash formhash = Regex.Match(s, @"(?<=formhash=)\w+").Value; // 获取留言信息,自动从163取新闻标题 bw.ReportProgress(1, "自动获取留言\r\n"); s = Encoding.Default.GetString(getBytes("http://news.163.com/special/0001220O/news_json.js", null, null)); string ly = Regex.Match(s, @"[\u4e00-\u9fa5]{20,}").Value; if (string.IsNullOrEmpty(ly)) ly = "快乐又一天~~~~,继续Happy~~~~~~"; bw.ReportProgress(1, " "+ly+"\r\n"); // 提交签到数据 p = string.Format("formhash={0}&qdxq=kx&qdmode=1&todaysay={1}&fastreply=1", formhash, HttpUtility.UrlEncode(ly)); postData = code.GetBytes(p); s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&sign_as=1&inajax=1", cc, postData)); //bw.ReportProgress(1, s); // 查看积分 bw.ReportProgress(1, "再次查看积分\r\n"); sb.Length = 0; s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/home.php?mod=spacecp&ac=credit&op=base", cc, null)); mc = Regex.Matches(s, @"<li[^<]*><em>(?<i>[^<]+)</em>(?<m>\d+)"); foreach (Match m in mc) { sb.AppendLine(string.Format("{0,20} {1}", m.Groups["i"].Value, m.Groups["m"].Value)); } bw.ReportProgress(1, sb.ToString() + "\r\n\r\n--------------------------------------------------------------------------------------------------------\r\n"); DateTime c=DateTime.Now; DateTime dt = c.Date.AddHours(27.5); Thread.Sleep(dt.Subtract(c)); } } void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { richTextBox1.AppendText(e.UserState.ToString()); } // 读取网络资源,返回字节数组 public byte[] getBytes(string url, CookieContainer cookie, byte[] postData) { byte[] data = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 30000; request.AllowAutoRedirect = true; if (cookie != null) request.CookieContainer = cookie; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"; request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; if (postData != null) // 需要 Post 数据 { request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postData.Length; try { Stream requestStream = request.GetRequestStream(); requestStream.Write(postData, 0, postData.Length); requestStream.Close(); } catch { return new byte[0]; } } else { request.Method = "GET"; } HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } if (response == null) return new byte[0]; string ce = response.Headers[HttpResponseHeader.ContentEncoding]; int ContentLength = (int)response.ContentLength; Stream s = response.GetResponseStream(); int c = 1024 * 10; if (ContentLength < 0) // 不能获取数据的长度 { data = new byte[c]; MemoryStream ms = new MemoryStream(); int l = s.Read(data, 0, c); while (l > 0) { ms.Write(data, 0, l); l = s.Read(data, 0, c); } data = ms.ToArray(); ms.Close(); } else // 数据长度已知 { data = new byte[ContentLength]; int pos = 0; while (ContentLength > 0) { int l = s.Read(data, pos, ContentLength); pos += l; ContentLength -= l; } } s.Close(); response.Close(); if (ce == "gzip") // 若数据是压缩格式,则要进行解压 { unRar(ref data); } return data; // 返回字节数组 } private byte[] unRar(ref byte[] data) // 解压数据 { try { MemoryStream js = new MemoryStream(); // 解压后的流 MemoryStream ms = new MemoryStream(data); // 用于解压的流 GZipStream g = new GZipStream(ms, CompressionMode.Decompress); byte[] buffer = new byte[10240]; // 读数据缓冲区 int l = g.Read(buffer, 0, 10240); // 一次读 10K while (l > 0) { js.Write(buffer, 0, l); l = g.Read(buffer, 0, 10240); } g.Close(); ms.Close(); data = js.ToArray(); js.Close(); return data; } catch { return data; } } private byte[] Rar(ref byte[] data) // 压缩数据 { MemoryStream ys = new MemoryStream(); // 压缩后的流 GZipStream g = new GZipStream(ys, CompressionMode.Compress); g.Write(data, 0, data.Length); g.Close(); data = ys.ToArray(); ys.Close(); return data; } }} 谢谢4楼的回帖,非常感谢你!Best wish for you!太好了,你的代码可以让我参考下,非常感谢,希望有问题能继续得到你的帮助! C#开发XML模版维护工具 三元操作符嵌套是不是会有问题? 线程问题,求指教 关于C#二维数组初始化问题 C#如何录制输出音频 关于判断网络页面是否存在的问题,谢谢! 像这样的版本怎么比较大小?用做在线升级(1.0.0.1)高分!! 求销售存代码 推荐 .Net的通讯组件Eastasp.Framework.Net 我怎么不能用System.Management??? c# 打包 WinForm会的进来看看
public class HttpHelper
{
public static CookieContainer Cookies = new CookieContainer();
public static string GetHttpResponse(string url, string postdata)
{
try
{
HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url);
MyRequest.Method = "POST";
MyRequest.ContentLength = postdata.Length;
MyRequest.CookieContainer = Cookies;
MyRequest.KeepAlive = true;
MyRequest.AllowAutoRedirect = true;
MyRequest.ContentType = "application/x-www-form-urlencoded";
MyRequest.UserAgent = " Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
MyRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; if (postdata != null)
{
ASCIIEncoding MyEncoding = new ASCIIEncoding();
byte[] MyByte = MyEncoding.GetBytes(postdata);
Stream MyStream = MyRequest.GetRequestStream();
MyStream.Write(MyByte, 0, postdata.Length);
MyStream.Close();
} string getContent = null;
Encoding myEncoding = Encoding.GetEncoding("UTF-8");
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
if (MyResponse.StatusCode == HttpStatusCode.OK)
{ /*获取响应页面cookie*/
// MyResponse.Cookies = MyRequest.CookieContainer.GetCookies(MyRequest.RequestUri); //CookieCollection cook = MyResponse.Cookies;
//string strCook = MyRequest.CookieContainer.GetCookieHeader(MyRequest.RequestUri);
//MessageBox.Show("adfdfasdf");
Stream MyNewStream = MyResponse.GetResponseStream();
StreamReader MyStreamReader = new StreamReader(MyNewStream, myEncoding);
getContent = MyStreamReader.ReadToEnd();
MyStreamReader.Close();
}
MyResponse.Close();
return getContent;
}
catch (Exception)
{
return string.Empty;
}
}
请问:登录后的cookie是保存在public static CookieContainer Cookies = new CookieContainer();中的Cookie中吗?这个Cookie是不是就是你说的做为访问登录页面需要的cookie呢?真的很希望得到你的帮助和回答,有范例代码就贴一贴吧!呵呵,再次感谢你!我是新手,上面的代码也不是很理解,这知道大概个原理和过程!
正是这个 CookieContainer。下面的登录3dezu网站并留言的代码,供参考:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using System.IO.Compression;
using System.Threading;namespace login3Dezu
{
public partial class Form1 : Form
{
CookieContainer cc = new CookieContainer();
string formhash = null;
BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerAsync();
} void bw_DoWork(object sender, DoWorkEventArgs e)
{
while (!bw.CancellationPending)
{
StringBuilder sb = new StringBuilder();
Encoding code = Encoding.ASCII; bw.ReportProgress(1,"当前日期:"+DateTime.Now.ToString()+"\r\n\r\n");
// 登录页面
bw.ReportProgress(1, "登录页面\r\n"); string p = "fastloginfield=username&username=XXXXX&password=XXXXX&quickforward=yes&handlekey=ls";
byte[] postData = code.GetBytes(p);
string s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1", cc, postData));
// 查看积分
bw.ReportProgress(1, "查看积分\r\n");
s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/home.php?mod=spacecp&ac=credit&op=base", cc, null));
MatchCollection mc = Regex.Matches(s, @"<li[^<]*><em>(?<i>[^<]+)</em>(?<m>\d+)");
foreach (Match m in mc)
{
sb.AppendLine(string.Format("{0,20} {1}", m.Groups["i"].Value, m.Groups["m"].Value));
}
bw.ReportProgress(1,sb.ToString()+"\r\n\r\n"); // 获取 formhash formhash = Regex.Match(s, @"(?<=formhash=)\w+").Value; // 获取留言信息,自动从163取新闻标题
bw.ReportProgress(1, "自动获取留言\r\n");
s = Encoding.Default.GetString(getBytes("http://news.163.com/special/0001220O/news_json.js", null, null));
string ly = Regex.Match(s, @"[\u4e00-\u9fa5]{20,}").Value;
if (string.IsNullOrEmpty(ly)) ly = "快乐又一天~~~~,继续Happy~~~~~~";
bw.ReportProgress(1, " "+ly+"\r\n");
// 提交签到数据 p = string.Format("formhash={0}&qdxq=kx&qdmode=1&todaysay={1}&fastreply=1", formhash, HttpUtility.UrlEncode(ly));
postData = code.GetBytes(p);
s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&sign_as=1&inajax=1", cc, postData));
//bw.ReportProgress(1, s); // 查看积分
bw.ReportProgress(1, "再次查看积分\r\n");
sb.Length = 0;
s = Encoding.UTF8.GetString(getBytes("http://www.3dezu.com/home.php?mod=spacecp&ac=credit&op=base", cc, null));
mc = Regex.Matches(s, @"<li[^<]*><em>(?<i>[^<]+)</em>(?<m>\d+)");
foreach (Match m in mc)
{
sb.AppendLine(string.Format("{0,20} {1}", m.Groups["i"].Value, m.Groups["m"].Value));
}
bw.ReportProgress(1, sb.ToString() + "\r\n\r\n--------------------------------------------------------------------------------------------------------\r\n");
DateTime c=DateTime.Now;
DateTime dt = c.Date.AddHours(27.5); Thread.Sleep(dt.Subtract(c));
}
} void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
richTextBox1.AppendText(e.UserState.ToString());
}
// 读取网络资源,返回字节数组
public byte[] getBytes(string url, CookieContainer cookie, byte[] postData)
{
byte[] data = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 30000;
request.AllowAutoRedirect = true;
if (cookie != null) request.CookieContainer = cookie;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; if (postData != null) // 需要 Post 数据
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
try
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
requestStream.Close();
}
catch
{
return new byte[0];
}
}
else
{
request.Method = "GET";
}
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
} if (response == null) return new byte[0]; string ce = response.Headers[HttpResponseHeader.ContentEncoding];
int ContentLength = (int)response.ContentLength;
Stream s = response.GetResponseStream();
int c = 1024 * 10;
if (ContentLength < 0) // 不能获取数据的长度
{
data = new byte[c];
MemoryStream ms = new MemoryStream();
int l = s.Read(data, 0, c);
while (l > 0)
{
ms.Write(data, 0, l);
l = s.Read(data, 0, c);
}
data = ms.ToArray();
ms.Close();
}
else // 数据长度已知
{
data = new byte[ContentLength];
int pos = 0;
while (ContentLength > 0)
{
int l = s.Read(data, pos, ContentLength);
pos += l;
ContentLength -= l;
}
}
s.Close();
response.Close(); if (ce == "gzip") // 若数据是压缩格式,则要进行解压
{
unRar(ref data);
}
return data; // 返回字节数组
} private byte[] unRar(ref byte[] data) // 解压数据
{
try
{
MemoryStream js = new MemoryStream(); // 解压后的流
MemoryStream ms = new MemoryStream(data); // 用于解压的流
GZipStream g = new GZipStream(ms, CompressionMode.Decompress);
byte[] buffer = new byte[10240]; // 读数据缓冲区
int l = g.Read(buffer, 0, 10240); // 一次读 10K
while (l > 0)
{
js.Write(buffer, 0, l);
l = g.Read(buffer, 0, 10240);
}
g.Close();
ms.Close();
data = js.ToArray();
js.Close();
return data;
}
catch
{
return data;
}
} private byte[] Rar(ref byte[] data) // 压缩数据
{
MemoryStream ys = new MemoryStream(); // 压缩后的流
GZipStream g = new GZipStream(ys, CompressionMode.Compress);
g.Write(data, 0, data.Length);
g.Close();
data = ys.ToArray();
ys.Close();
return data;
}
}
}