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网页信息的更多相关文章

  1. Request获取url各种信息的方法

    1.Request获取url各种信息的方法 测试的url地址:http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPa ...

  2. C# winfrom HttpWebRequest 请求获取html网页信息和提交信息

    string result =GetRequest("http://localhost:32163/DuoBao/ajax.aspx", "time=5"); ...

  3. Spring获取URL相关信息

    获取请求的URL:request.getRequestURL().toString(); 获取上下文名称(项目名称):request.getContextPath()

  4. 在python使用selenium获取动态网页信息并用BeautifulSoup进行解析--动态网页爬虫

    爬虫抓取数据时有些数据是动态数据,例如是用js动态加载的,使用普通的urllib2 抓取数据是找不到相关数据的,这是爬虫初学者在使用的过程中,最容易发生的情况,明明在浏览器里有相应的信息,但是在pyt ...

  5. selenium获取动态网页信息(某东)-具体配置信息

    需要安装的包: selenium 关于软件的驱动:selenium之 驱动环境配置chrome.firefox.IE 1 # encoding:utf-8 2 # Author:"richi ...

  6. Python爬虫初探 - selenium+beautifulsoup4+chromedriver爬取需要登录的网页信息

    目标 之前的自动答复机器人需要从一个内部网页上获取的消息用于回复一些问题,但是没有对应的查询api,于是想到了用脚本模拟浏览器访问网站爬取内容返回给用户.详细介绍了第一次探索python爬虫的坑. 准 ...

  7. C# HttpWebRequest 绝技 根据URL地址获取网页信息

    如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...

  8. jsp Request获取url信息的各种方法比较

    从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...

  9. 使用URLConnection获取网页信息的基本流程

    参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...

随机推荐

  1. 在iis搭建nuget server时遇到405 method not allow

    <configuration> <system.webServer> <validation validateIntegratedModeConfiguration=&q ...

  2. [转]C# 关闭嵌在程序中的word进程而不关闭用户通过word手动打开的word进程

    命名空间 :System.Diagnostics 以前在word的时候,经常碰到word进程产生一大堆,怕关错了,把用户自己打开的word也关闭,一直搞忽悠,今天上网花了10块钱,下了个文件,给我了一 ...

  3. java后台向路径发送请求获得相应参数

    从java后台向一路径发送请求,获得响应的参数,put get post ,还有一个返回URL的工具类,方便代码灵活修改 import java.io.BufferedReader; import j ...

  4. 彻底弄懂HTTP缓存机制及原理(转载)

    https://www.cnblogs.com/chenqf/p/6386163.html 前言 Http 缓存机制作为 web 性能优化的重要手段,对于从事 Web 开发的同学们来说,应该是知识体系 ...

  5. 设计模式--单例模式(学习Learning hard大神笔记实践)

    根据大神博客园中的文章,自己亲手敲了一遍,对每个解说点都自己动手进行实践,收获颇丰,谢谢Learning hard大神,原文地址http://www.cnblogs.com/zhili/p/Desig ...

  6. js实现放大镜的效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. display:inline-block居中方式

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. 安卓脱壳&&协议分析&&burp辅助分析插件编写

    前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 前言 本文以一个 app 为例,演示对 app脱壳,然后分析其 协 ...

  9. syscall to rop

    前言 hitcon 2017 的 start 题,比较简单,练练手. 题目链接: https://gitee.com/hac425/blog_data/tree/master/hitcon2017 正 ...

  10. JAVA 分布式 - 分布式介绍

    什么是分布式系统? 要理解分布式系统,主要需要明白一下2个方面: 1.分布式系统一定是由多个节点组成的系统. 其中,节点指的是计算机服务器,而且这些节点一般不是孤立的,而是互通的. 2.这些连通的节点 ...