给你一个方法,返回的是网页的html代码! public string GetUrl_Text(string urls,string SeverName) { string localDir = @"E:\Website"; if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); Console.WriteLine("Directory {0} Created!!!", localDir); } //Uncomment the next 2 lines to save cookies from the site CookieContainer cookieContainer = new CookieContainer(); Uri bbsRoot = new Uri(SeverName); string url = urls; Uri target = new Uri(url); try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(target); request.AllowAutoRedirect = false; request.Accept = "*/*"; request.Headers.Add("Accept-Language", "zh-cn"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //Uncomment the next 4 lines to use a proxy WebProxy myProxy = new WebProxy(); Uri newUri=new Uri(SeverName); myProxy.Address = newUri; request.Proxy = myProxy; //Uncomment the next 4 lines to save cookies from the site if (cookieContainer.Count > 0) { request.CookieContainer = cookieContainer; } string content = ""; using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { //Uncomment the next 4 lines to save cookies from the site if (response.Headers["SetCookies"] != null && response.Headers["SetCookies"] != string.Empty) { cookieContainer.SetCookies(bbsRoot,response.Headers["SetCookies"]); } using(StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default, true)) { content = sr.ReadToEnd(); } } return content; } catch (WebException e) { return e.Message; } catch (IOException e) { return e.Message; } }
你的有Session传递! 这个就只有传递Cookies! 我改了一些!下面是最后的代码! public string GetUrl_Text(string urls,string SeverName) { //Uncomment the next 2 lines to save cookies from the site CookieContainer cookieContainer = new CookieContainer(); Uri bbsRoot = new Uri(SeverName); Cookie Cookies1=new Cookie("Chars","Bit5");//创建Cookie cookieContainer.Add(bbsRoot,Cookies1);//添加Cookie string url = urls; Uri target = new Uri(url); try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(target); request.AllowAutoRedirect = false; request.Accept = "*/*"; request.Headers.Add("Accept-Language", "zh-cn"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //Uncomment the next 4 lines to use a proxy WebProxy myProxy = new WebProxy(); Uri newUri=new Uri(SeverName); myProxy.Address = newUri; request.Proxy = myProxy; //Uncomment the next 4 lines to save cookies from the site if (cookieContainer.Count > 0) { request.CookieContainer = cookieContainer; } string content = ""; using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { //Uncomment the next 4 lines to save cookies from the site if (response.Headers["SetCookies"] != null && response.Headers["SetCookies"] != string.Empty) { cookieContainer.SetCookies(bbsRoot,response.Headers["SetCookies"]); } using(StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default, true)) { content = sr.ReadToEnd(); } } return content; //StreamWriter sw = new StreamWriter(localDir + "\\Page.htm", false, System.Text.Encoding.Default); //sw.Write(content); //sw.Close(); //Console.WriteLine("oh ye~~ : {0} download!!!", localDir + "\\Page.html"); } catch (WebException e) { return e.Message; } catch (IOException e) { return e.Message; } } }================================ 我测试以后是正确的!Response.Write(GetUrl_Text("http://www.wise111.com/Union_Services.asp","http://www.wise111.com"));
刚测试了下,fancyf(凡瑞) 大哥, 当a.aspx===>transfer到b.aspx文件 我在b.aspx执行 public string GetUrl_Text(string urls,string SeverName) { //Uncomment the next 2 lines to save cookies from the site CookieContainer cookieContainer = new CookieContainer(); Uri bbsRoot = new Uri(SeverName); Cookie Cookies1=new Cookie("Chars","Bit5");//创建Cookie cookieContainer.Add(bbsRoot,Cookies1);//添加Cookie string url = urls; Uri target = new Uri(url); try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(target); request.AllowAutoRedirect = false; request.Accept = "*/*"; request.Headers.Add("Accept-Language", "zh-cn"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //Uncomment the next 4 lines to use a proxy WebProxy myProxy = new WebProxy(); Uri newUri=new Uri(SeverName); myProxy.Address = newUri; request.Proxy = myProxy; //Uncomment the next 4 lines to save cookies from the site if (cookieContainer.Count > 0) { request.CookieContainer = cookieContainer; } string content = ""; using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { //Uncomment the next 4 lines to save cookies from the site if (response.Headers["SetCookies"] != null && response.Headers["SetCookies"] != string.Empty) { cookieContainer.SetCookies(bbsRoot,response.Headers["SetCookies"]); } using(StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default, true)) { content = sr.ReadToEnd(); } } return content; //StreamWriter sw = new StreamWriter(localDir + "\\Page.htm", false, System.Text.Encoding.Default); //sw.Write(content); //sw.Close(); //Console.WriteLine("oh ye~~ : {0} download!!!", localDir + "\\Page.html"); } catch (WebException e) { return e.Message; } catch (IOException e) { return e.Message; } }函数 如果是请求a.aspx的话,则得到是a.aspx的文件,取不到b.aspx文件。 设为b.aspx也是不行。另外还有一个问题就是那些gif图片怎么修改成绝对连接啊? 请高手帮帮我。谢谢
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
DataSet ds = new DataSet();
WebClient wc = new WebClient();
byte[] bt = wc.DownloadData(xmlPath); XmlDocument xd = new XmlDocument();string source = Byte2String(bt);需要引入System.Net
http://community.csdn.net/Expert/TopicView.asp?id=4066881
把里面注释掉的几段程序恢复就可以了
现有几个问题,还请你帮忙解释下。
因为我要获取的B.ASPX页面,是从A.aspx传过去的,而传过去的是session,而不是通过b.aspx?parameter=???这方式传过去的。
用string url = "http://bbs.wowchina.com/bbs/bbs_list.aspx?forum_id=4&page_no=" + mPage;
Uri target = new Uri(url);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(target);
HttpWebResponse response = (HttpWebResponse)request.GetResponse()
这样获取的页面没有session的吧。那这么得到的结果肯定不是我想要的页面。因为里面处理的数据都不对。
还请fancyf(凡瑞)大哥再指点下小弟。万分感谢。
不过我看你用Server.Transfer来转换页面的,客户段并没有任何变化
你用WebClient直接下载a.aspx的url所得到的内容就是Server.Transfer之后的b.aspx的内容
public string GetUrl_Text(string urls,string SeverName)
{
string localDir = @"E:\Website"; if (!Directory.Exists(localDir))
{
Directory.CreateDirectory(localDir);
Console.WriteLine("Directory {0} Created!!!", localDir);
} //Uncomment the next 2 lines to save cookies from the site
CookieContainer cookieContainer = new CookieContainer();
Uri bbsRoot = new Uri(SeverName); string url = urls;
Uri target = new Uri(url);
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(target);
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.Headers.Add("Accept-Language", "zh-cn");
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //Uncomment the next 4 lines to use a proxy
WebProxy myProxy = new WebProxy();
Uri newUri=new Uri(SeverName);
myProxy.Address = newUri;
request.Proxy = myProxy; //Uncomment the next 4 lines to save cookies from the site
if (cookieContainer.Count > 0)
{
request.CookieContainer = cookieContainer;
} string content = "";
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
//Uncomment the next 4 lines to save cookies from the site
if (response.Headers["SetCookies"] != null && response.Headers["SetCookies"] != string.Empty)
{
cookieContainer.SetCookies(bbsRoot,response.Headers["SetCookies"]);
} using(StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default, true))
{
content = sr.ReadToEnd();
} } return content;
}
catch (WebException e)
{
return e.Message;
}
catch (IOException e)
{
return e.Message;
}
}
这个就只有传递Cookies!
我改了一些!下面是最后的代码!
public string GetUrl_Text(string urls,string SeverName)
{
//Uncomment the next 2 lines to save cookies from the site
CookieContainer cookieContainer = new CookieContainer();
Uri bbsRoot = new Uri(SeverName); Cookie Cookies1=new Cookie("Chars","Bit5");//创建Cookie cookieContainer.Add(bbsRoot,Cookies1);//添加Cookie string url = urls;
Uri target = new Uri(url);
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(target);
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.Headers.Add("Accept-Language", "zh-cn");
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //Uncomment the next 4 lines to use a proxy
WebProxy myProxy = new WebProxy();
Uri newUri=new Uri(SeverName);
myProxy.Address = newUri;
request.Proxy = myProxy; //Uncomment the next 4 lines to save cookies from the site
if (cookieContainer.Count > 0)
{
request.CookieContainer = cookieContainer;
} string content = "";
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
//Uncomment the next 4 lines to save cookies from the site
if (response.Headers["SetCookies"] != null && response.Headers["SetCookies"] != string.Empty)
{
cookieContainer.SetCookies(bbsRoot,response.Headers["SetCookies"]);
} using(StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default, true))
{
content = sr.ReadToEnd();
} } return content;
//StreamWriter sw = new StreamWriter(localDir + "\\Page.htm", false, System.Text.Encoding.Default);
//sw.Write(content);
//sw.Close();
//Console.WriteLine("oh ye~~ : {0} download!!!", localDir + "\\Page.html");
}
catch (WebException e)
{
return e.Message;
}
catch (IOException e)
{
return e.Message;
}
} }================================
我测试以后是正确的!Response.Write(GetUrl_Text("http://www.wise111.com/Union_Services.asp","http://www.wise111.com"));
当a.aspx===>transfer到b.aspx文件
我在b.aspx执行
public string GetUrl_Text(string urls,string SeverName)
{
//Uncomment the next 2 lines to save cookies from the site
CookieContainer cookieContainer = new CookieContainer();
Uri bbsRoot = new Uri(SeverName); Cookie Cookies1=new Cookie("Chars","Bit5");//创建Cookie cookieContainer.Add(bbsRoot,Cookies1);//添加Cookie string url = urls;
Uri target = new Uri(url);
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(target);
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.Headers.Add("Accept-Language", "zh-cn");
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //Uncomment the next 4 lines to use a proxy
WebProxy myProxy = new WebProxy();
Uri newUri=new Uri(SeverName);
myProxy.Address = newUri;
request.Proxy = myProxy; //Uncomment the next 4 lines to save cookies from the site
if (cookieContainer.Count > 0)
{
request.CookieContainer = cookieContainer;
} string content = "";
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
//Uncomment the next 4 lines to save cookies from the site
if (response.Headers["SetCookies"] != null && response.Headers["SetCookies"] != string.Empty)
{
cookieContainer.SetCookies(bbsRoot,response.Headers["SetCookies"]);
} using(StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default, true))
{
content = sr.ReadToEnd();
} } return content;
//StreamWriter sw = new StreamWriter(localDir + "\\Page.htm", false, System.Text.Encoding.Default);
//sw.Write(content);
//sw.Close();
//Console.WriteLine("oh ye~~ : {0} download!!!", localDir + "\\Page.html");
}
catch (WebException e)
{
return e.Message;
}
catch (IOException e)
{
return e.Message;
}
}函数
如果是请求a.aspx的话,则得到是a.aspx的文件,取不到b.aspx文件。
设为b.aspx也是不行。另外还有一个问题就是那些gif图片怎么修改成绝对连接啊?
请高手帮帮我。谢谢
你的意思是否是,我不要用session来传递,用cookiee来传递?
我试了下你的方法,结果也是一样的。
a.aspx===>b.aspx
传输了session("userid")
所以
咳,还请高手解决。
http://lovecherry.cnblogs.com/archive/2005/06/18/176791.html
有用吗
===========
就是不要用Session,改成Cookie来传递!我测试都是行的!
我上面发的那个函数!
Cookie Cookies1=new Cookie("Chars","Bit5");//创建Cookie
cookieContainer.Add(bbsRoot,Cookies1);//添加Cookie意思就是建立一个Cookie名字为:Chars,值为:Bit5的Cookie!
并添加到cookieContainer里面去!我添加的Cookie是对应的:Response.Write(GetUrl_Text("http://www.wise111.com/Union_Services.asp","http://www.wise111.com"));这个网站的抓取!
这个网站是用Cookie值来判定显示繁体还是简体的!我测试都是可以的!你要添加什么样的Cookie就自己做一下修改!这个方法还可以改进一下的!==========
要想严谨一点的话,最好在你提取的页面里的图片路径就用绝对的地址!
因为项目一早就用了session,也不想改了。
谢谢各位大哥们。
结贴。