获取URL网页信息
static string GetHtml(string url)
{string strHTML = "";
WebClient myWebClient = new WebClient();
Stream myStream = myWebClient.OpenRead(url);
StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
strHTML = sr.ReadToEnd();
myStream.Close();
return strHTML;
} static bool GetHtml(string url)
{ HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Timeout = * ; //连接超时
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream stream = myHttpWebResponse.GetResponseStream();
StreamReader sr =
new StreamReader(stream, Encoding.GetEncoding("utf-8"));
var html = sr.ReadToEnd();
}
获取当前Cookies保存后获取整站信息
static void Main(string[] args)
{ Console.WriteLine("确保挂载VPN后。。。回车键继续!");
Console.ReadKey(); string url = "http://www.sciencedirect.com/science/journal/00118486/61/6"; string indata = "aa=zhuye";
CookieContainer myCookieContainer =
new CookieContainer();
//新建一个cookiecontainer来存放cookie集合
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create(url);
//新建一个httpwebrequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "post";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置httpwebrequest的cookiecontainer为
//刚才建立的那个mycookiecontainer
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入httpwebrequest的request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
//新建一个httpwebresponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri); Console.WriteLine("获取cookies成功!。。。关闭VPN后任意键继续");
Console.ReadKey(); var htmlData = SaveCook(myHttpWebRequest, prevUrl, myCookieContainer, myHttpWebResponse); }
}
public static string SaveCook(HttpWebRequest myHttpWebRequest, string url, CookieContainer myCookieContainer, HttpWebResponse myHttpWebResponse)
{
//拿到了cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*
//刚才那个cookiecontainer已经存有了cookie,把它附加到 myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies =
myCookieContainer.GetCookies(myHttpWebRequest.RequestUri); Stream myresponsestream = myHttpWebResponse.GetResponseStream();
StreamReader mystreamreader =
new StreamReader(myresponsestream, Encoding.GetEncoding("gb2312")); var html = mystreamreader.ReadToEnd();
//把数据从httpwebresponse的response流中读出
mystreamreader.Close();
myresponsestream.Close();
return html; }
获取URL网页信息的更多相关文章
- Request获取url各种信息的方法
1.Request获取url各种信息的方法 测试的url地址:http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPa ...
- C# winfrom HttpWebRequest 请求获取html网页信息和提交信息
string result =GetRequest("http://localhost:32163/DuoBao/ajax.aspx", "time=5"); ...
- Spring获取URL相关信息
获取请求的URL:request.getRequestURL().toString(); 获取上下文名称(项目名称):request.getContextPath()
- 在python使用selenium获取动态网页信息并用BeautifulSoup进行解析--动态网页爬虫
爬虫抓取数据时有些数据是动态数据,例如是用js动态加载的,使用普通的urllib2 抓取数据是找不到相关数据的,这是爬虫初学者在使用的过程中,最容易发生的情况,明明在浏览器里有相应的信息,但是在pyt ...
- selenium获取动态网页信息(某东)-具体配置信息
需要安装的包: selenium 关于软件的驱动:selenium之 驱动环境配置chrome.firefox.IE 1 # encoding:utf-8 2 # Author:"richi ...
- Python爬虫初探 - selenium+beautifulsoup4+chromedriver爬取需要登录的网页信息
目标 之前的自动答复机器人需要从一个内部网页上获取的消息用于回复一些问题,但是没有对应的查询api,于是想到了用脚本模拟浏览器访问网站爬取内容返回给用户.详细介绍了第一次探索python爬虫的坑. 准 ...
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...
- jsp Request获取url信息的各种方法比较
从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...
- 使用URLConnection获取网页信息的基本流程
参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...
随机推荐
- NOI2018 退役记
退役预订... upd 果然就这么不光荣的退役了... 我居然考出了一场只有两题得分的比赛,我好菜啊... 不过高三充(tui)实(fei)的生活应该很有意思... 大家一起加油吧!!!
- 数组的strong copy理解
一.数组的不同情况下的copy,mutablecopy分析 1.不可变数组的copy(没有创建新对象,复制的只是指针) 2.不可变数组的mutable copy(创建新对象) ...
- VFL使用
关于界面布局约束的方法有很多,纯代码布局,可以使用官方原生布局(很繁琐).VFL.Masonary第三方等,在xib或者storyboard中也可以使用Autolayout的界面约束进行布局约束. ...
- 从一个带有http://网络文件中获取文件名
例如文件网址为: http://a.hiphotos.baidu.com/image/h%3D200/sign=c3da85e50123dd543e73a068e108b3df/80cb39dbb6f ...
- 一:Web
一:Web介绍 1.Web:网页,网络 2.Web应用:运行在网络上的应用程序 3.网络应用程序分类:C/S B/S C/S 客服端(client)/服务器(server) 典型应用:QQ,YY B/ ...
- 哈夫曼编码(Huffman coding)的那些事,(编码技术介绍和程序实现)
前言 哈夫曼编码(Huffman coding)是一种可变长的前缀码.哈夫曼编码使用的算法是David A. Huffman还是在MIT的学生时提出的,并且在1952年发表了名为<A Metho ...
- 个人遗漏知识的回顾-HTML
常用的一些快捷键: Windows + e 我的电脑Ctrl + Tab 网页间不同页面切换F2 重命名Ctrl+Shift+S 另存为 前端的一些常识:前端意义:将效果图生成网页网页组成:文字.图片 ...
- 容易忽略的小知识之 Date 函数参数
Date 对象启用基本存储器并取得日期和时间. dateObj = new Date()dateObj = new Date(dateVal)dateObj = new Date(year, mont ...
- c#如何禁用win7的任务管理器
以前制作的桌面锁屏软件虽然也禁用过任务管理器,但是采取的是比较笨的方法,而且对操作系统还有一定的危害.因为任务管理也是一个窗体也就是说它中也是一个独立进程,所以只需要强制性关闭这个进程即可以关闭任务管 ...
- opencv之图像拼接
参考博客http://blog.csdn.net/u011630458/article/details/44175965 博主:羽凌寒 之后再进行系统学习