诚心 求教c#登陆网站获取网页及链接网页数据
使用C#登陆带用户名和密码的网站,并获得网页源码;这个问题已解决!部分代码如下:关键是这个登陆的这个网站有很多链接,我想获得这些链接网页(即子网页)的源码怎么做?
谢谢各位帮忙!小弟急用!
// 获取数据时调用
private void getPage(String url, String paramList)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
string cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
req.CookieContainer.SetCookies(new Uri(url), cookieheader);
byte[] SomeBytes = Encoding.UTF8.GetBytes(paramList);
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
byte[] buffer = new byte[1024];
string filename = @"c:\1\source.html";
Stream outStream = File.Create(filename);
int l;
do
{
l = ReceiveStream.Read(buffer, 0, buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
} while (l > 0);
outStream.Close();
}
--------------------编程问答-------------------- 请求子网页时,附加Cookie
....
CookieContainer cookieCon = new CookieContainer();
cookieCon.SetCookies(new Uri( url) , "Url关联的Cookie字符串");
req.CookieContainer = cookieCon;
--------------------编程问答--------------------
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
StreamReader reader=new StreamReader(ReceiveStream);//套接StreamReader
String source=reader.ReadToEnd();//一次读取所有相应内容
............
Stream outStream = File.Create(filename);
StreamWriter writer=new StreamWriter(outStream);//套接StreamWriter
writer.Write(source);//将读取到的内容一次写入文件
outStream.Close();
writer.Close();
这样似乎效率比你的更高。
--------------------编程问答-------------------- 关注一下.帮顶一下! --------------------编程问答-------------------- 顶顶更健康 --------------------编程问答--------------------
补充:.NET技术 , C#