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. 浅析人脸检测之Haar分类器方法:Haar特征、积分图、 AdaBoost 、级联

    浅析人脸检测之Haar分类器方法 一.Haar分类器的前世今生 人脸检测属于计算机视觉的范畴,早期人们的主要研究方向是人脸识别,即根据人脸来识别人物的身份,后来在复杂背景下的人脸检测需求越来越大,人脸 ...

  2. [转][C#] 对List<T>取交集、连集及差集

    本文转自:http://www.cnblogs.com/shuibin/archive/2012/04/19/2457867.html 最近在專案中,剛好遇到這個需求, 需要比對兩個List,進行一些 ...

  3. ABP学习入门系列(四)(创建Service)

    一,一些相关解释 Service 在应用服务层也就是application层.应用服务用于将领域(业务)逻辑暴露给展现层.展现层通过传入DTO(数据传输对象)参数来调用应用服务,而应用服务通过领域对象 ...

  4. asp后台读id设置样式

    加runat=“server”

  5. MyEclipse设置当前行背景颜色、选中单词前景色、背景色

    General->Editors->Text Editors -> Current Line Hightlight 可以设置当前行的背景颜色 效果如下:

  6. iOS交互h5——user-agent

    User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串. 在公司产品中,在userAgent中增加了XX ...

  7. java利器------反射机制

    java反射的概念:java的反射机制是指在运行状态下,对于一个类来说,可以得到这个类的所有方法和属性.对于一个对象来说,可以调用这个对象的人和方法和属性. 反射机制首先会拿到该类的字节码文件(Cla ...

  8. FileReader读取文件

    前言:FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. < ...

  9. Django REST Framework应用

    一. 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角 ...

  10. intent调用代码总结

    进入联系人界面 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(People.CO ...