各位,请问我想在程序中访问需要登陆的网站的源代码,在程序中webbrowser登陆后,自动后台察看本站源码。
我采用一些朋友说的保存cookie,附加到请求中,可行,但是有些网站读不到cookie,或cookie有问题,读不全的问题。
//在WebBrowser中登录 cookie保存在 WebBrowser.Document.Cookie中
CookieContainer myCookieContainer = new CookieContainer(); //String 的Cookie 要转成 Cookie型的 并放入CookieContainer中
string cookieStr = webBrowser1.Document.Cookie;
string[] cookstr = cookieStr.Split(';');
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
ck.Domain = "www.abc.com";//必须写对
myCookieContainer.Add(ck);
} HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp");
hreq.Method = "POST";
hreq.ContentType = "application/x-www-form-urlencoded";
//自己创建的CookieContainer
hreq.CookieContainer = myCookieContainer;
string postdata = "id=2005&action=search&name=";
byte[] byte1 = Encoding.ASCII.GetBytes(postdata);
hreq.ContentLength = byte1.Length;
Stream poststream = hreq.GetRequestStream();
poststream.Write(byte1, 0, byte1.Length);
poststream.Close();
HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
有些问题:如cookie:ASPSESSIONIDSSCTBTDS=MJAPHPLCKDLMOHGKNBIMLMPD; company=password=lvtong&id=2032不能写入,
另外还有没有别的思路来保留登陆状态!
我采用一些朋友说的保存cookie,附加到请求中,可行,但是有些网站读不到cookie,或cookie有问题,读不全的问题。
//在WebBrowser中登录 cookie保存在 WebBrowser.Document.Cookie中
CookieContainer myCookieContainer = new CookieContainer(); //String 的Cookie 要转成 Cookie型的 并放入CookieContainer中
string cookieStr = webBrowser1.Document.Cookie;
string[] cookstr = cookieStr.Split(';');
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
ck.Domain = "www.abc.com";//必须写对
myCookieContainer.Add(ck);
} HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp");
hreq.Method = "POST";
hreq.ContentType = "application/x-www-form-urlencoded";
//自己创建的CookieContainer
hreq.CookieContainer = myCookieContainer;
string postdata = "id=2005&action=search&name=";
byte[] byte1 = Encoding.ASCII.GetBytes(postdata);
hreq.ContentLength = byte1.Length;
Stream poststream = hreq.GetRequestStream();
poststream.Write(byte1, 0, byte1.Length);
poststream.Close();
HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
有些问题:如cookie:ASPSESSIONIDSSCTBTDS=MJAPHPLCKDLMOHGKNBIMLMPD; company=password=lvtong&id=2032不能写入,
另外还有没有别的思路来保留登陆状态!
解决方案 »
- C# 高效获取网页源码 再提取网页正文内容
- C#中对打印机的操作问题
- for循环里面Process.start出错!急,在线等!
- System.IO.Directory.Exists用asp怎么实现?
- 如何从数据库中模糊查询,获得我要的值
- C# TextBox子类化问题
- 上传文件到 Implicit SSL 的 FTP,有没有免费的控件啊???
- SQL SERVER2000 数据库如何与oracle数据库交换数据
- WPF程序,如何在当签窗口遍历子窗口的所有控件呢?比如说我有一个导航界面,选择不同按钮就会有不同的page,我怎么去遍历page里的控件呢?
- 怎样发送中文写的邮件?
- 哪种源代码管理器比较安全?VSS安全性太差了。
- winform读取和配置CSS式样
如果写不成,为什么不用socket,为什么非要使用HttpWebRequest呢!
是做网络蜘蛛的吧!
你以为现成的就那么好用!
还有的网站,cookie是动态变化的,有的是通过网页中的javascript来设置的!
你需要分情况!
上面的问题,你就不要使用 hreq.CookieContainer = myCookieContainer; 了,
HttpWebRequest里边不是还有header.add方法吗?逐步添加吧!
可以大概介绍下socket在这方面的使用吗?
............不是我不告诉你,貌似:“还可以,我知道了,谢谢” 不是我写的。因为这个号,别人也在上。另外,我不太会用WebBrower,我用HttpWebRequest...
webbrowser.document.cookie是空的,怎么回事?
我就在上面登陆的,却得不到东西,怎么办?
被你打败了。 很抱歉,没有仔细看你的问题。另外,你可以看一下这段代码: string strLoginUrl = "提交地址";
string strLoginUrlPost = "提交内容"; HttpWebRequest reqLogin = (HttpWebRequest)WebRequest.Create(strLoginUrl);
reqLogin.KeepAlive = true;
reqLogin.Method = "POST";
reqLogin.AllowAutoRedirect = true;
reqLogin.UseDefaultCredentials = true;
// reqLogin.Referer = strLoginPageUrlReferer;
reqLogin.UserAgent ="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 1.1.4322; .NET CLR 3.5.20404)";
reqLogin.ContentType = "application/x-www-form-urlencoded";
reqLogin.CookieContainer = new CookieContainer();
//reqLogin.CookieContainer.Add(ccFirstPage);
reqLogin.ContentLength = strLoginUrlPost.Length;
//POST 提交
StreamWriter sw2 = new StreamWriter(reqLogin.GetRequestStream());
sw2.Write(strLoginUrlPost);
sw2.Close();
//得到提交之后的返回数据
HttpWebResponse resLogin = (HttpWebResponse)reqLogin.GetResponse();
resLogin.Cookies = reqLogin.CookieContainer.GetCookies(reqLogin.RequestUri);
//返回的Header
Debug.Write("\n Header >> " + reqLogin.Headers.ToString());
StreamReader sr2 = new StreamReader(resLogin.GetResponseStream());
String strAftLoginPage = sr2.ReadToEnd();
sr2.Close();
CookieCollection ccLogin = new CookieCollection();
ccLogin.Add(resLogin.Cookies);
//response url url
string strREsponseUrlTemp = resLogin.ResponseUri.ToString();
resLogin.Close();
reqLogin.Abort();
必有重谢!!!!