获取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 ...
随机推荐
- 008.在C#中,显式接口VS隐式接口
原文http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp (At ...
- 高并发第十三弹:J.U.C 队列 SynchronousQueue.ArrayBlockingQueue.LinkedBlockingQueue.LinkedTransferQueue
因为下一节会说线程池,要用线程池 那么线程池有个很重要的参数 就是Queue的选择 常用的队列其实就两种: 先进先出(FIFO):先插入的队列的元素也最先出队列,类似于排队的功能.从某种程度上来说这种 ...
- JSP学习笔记(5)-Java Servlet
1.什么是Servlet? Servlet(Servlet Applet),全程Java Servlet,是用Java编写的服务器端程序,其主要功能在于交互式地浏览和修改数据,生成动态WEB内容.侠义 ...
- 网络编程: 基于UDP协议的socket
udp是无链接的,启动服务之后可以直接接受消息,不需要提前建立链接 UDP协议的通信优势: 允许一个服务器同时和多个客户端通信, TCP不行 服务端 import socket sk = socket ...
- springJDBC 中JdbcTemplate 类方法使用
一,Dao IUserinfDao package com.dkt.dao; import java.util.List; import com.dkt.entity.Userinfo; public ...
- 基于token的后台身份验证(转载)
几种常用的认证机制 HTTP Basic Auth HTTP Basic Auth简单点说明就是每次请求API时都提供用户的username和password,简言之,Basic Auth是配合RES ...
- Google JavaScript样式指南
Google JavaScript样式指南 目录 1简介 1.1术语说明 1.2指南说明 2源文件基础知识 2.1文件名 2.2文件编码:UTF-8 2.3特殊字符 3源文件结构 3.1许可或版权 ...
- JS 面试题 实践一
1.完成 extname 函数,它会接受一个文件名作为参数,你需要返回它的扩展名. 例如,输入 emoji.png,返回 .png. const extname = (filename) => ...
- ciscn2018-pwn-wp
前言 2018全国大学生网络安全竞赛 ,做了2 道题 task_supermarket change_desc 里面调用 realloc 会触发 uaf 利用 uaf 修改 obj->desc_ ...
- 解决提交项目到GitHub,报错Please tell me who you are的问题
描述: 在第一次提交项目到GitHub时,出现了以下问题 看了一下错误原因:Run git config --global user.email "you@example.com" ...