标签:
如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取
1.第一招,根据URL地址获取网页信息
先来看一下代码
get方法
C# Code复制内容到剪贴板
- public static string GetUrltoHtml(string Url,string type)
-
- {
-
- try
-
- {
-
- System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
-
-
-
- System.Net.WebResponse wResp = wReq.GetResponse();
-
- System.IO.Stream respStream = wResp.GetResponseStream();
-
-
-
- using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type)))
- {
- return reader.ReadToEnd();
- }
-
- }
-
- catch (System.Exception ex)
-
- {
-
-
-
- }
-
- return "";
-
- }
post方法
C# Code复制内容到剪贴板
-
-
-
-
-
-
-
-
-
-
-
- public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
-
- {
-
- Encoding encoding = Encoding.Default;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
-
- request.Method = "post";
-
- request.Accept = "text/html, application/xhtml+xml, */*";
-
- request.ContentType = "application/x-www-form-urlencoded";
-
- byte[] buffer = encoding.GetBytes(strPostdata);
-
- request.ContentLength = buffer.Length;
-
- request.GetRequestStream().Write(buffer, 0, buffer.Length);
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
- using( StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
- {
-
- return reader.ReadToEnd();
- }
-
- }
这招是入门第一式, 特点:
1.最简单最直观的一种,入门课程。
2.适应于明文,无需登录,无需任何验证就可以进入的页面。
3.获取的数据类型为HTML文档。
4.请求方法为Get/Post
2.第二招,根据URL地址获取需要验证证书才能访问的网页信息
先来看一下代码
get方法
C# Code复制内容到剪贴板
-
- public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
-
- {
-
- return true;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string GetUrltoHtml(string Url)
-
- {
-
- StringBuilder content = new StringBuilder();
-
-
-
- try
-
- {
-
-
-
- ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
-
-
-
-
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
-
-
-
-
-
- X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer");
-
-
-
-
-
- request.ClientCertificates.Add(objx509);
-
-
-
-
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
-
-
- Stream responseStream = response.GetResponseStream();
-
-
-
- StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
-
-
-
- Char[] sReaderBuffer = new Char[256];
-
- int count = sReader.Read(sReaderBuffer, 0, 256);
-
- while (count > 0)
-
- {
-
- String tempStr = new String(sReaderBuffer, 0, count);
-
- content.Append(tempStr);
-
- count = sReader.Read(sReaderBuffer, 0, 256);
-
- }
-
-
-
- sReader.Close();
-
- }
-
- catch (Exception)
-
- {
-
- content = new StringBuilder("Runtime Error");
-
- }
-
-
-
- return content.ToString();
-
-
-
- }
post方法
C# Code复制内容到剪贴板
-
- public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
-
- {
-
-
-
- return true;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
-
- {
-
-
-
- ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
-
- Encoding encoding = Encoding.Default;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
-
-
-
-
-
- X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer");
-
-
-
-
-
- request.CookieContainer = new CookieContainer();
-
-
-
-
-
- request.ClientCertificates.Add(objx509);
-
- request.Method = "post";
-
- request.Accept = "text/html, application/xhtml+xml, */*";
-
- request.ContentType = "application/x-www-form-urlencoded";
-
- byte[] buffer = encoding.GetBytes(strPostdata);
-
- request.ContentLength = buffer.Length;
-
- request.GetRequestStream().Write(buffer, 0, buffer.Length);
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
- {
-
- return reader.ReadToEnd();
- }
-
- }
这招是学会算是进了大门了,凡是需要验证证书才能进入的页面都可以使用这个方法进入,我使用的是证书回调验证的方式,证书验证是否通过在客户端验证,这样的话我们就可以使用自己定义一个方法来验证了,有的人会说那也不清楚是怎么样验证的啊,其它很简单,代码是自己写的为什么要那么难为自己呢,直接返回一个True不就完了,永远都是验证通过,这样就可以无视证书的存在了, 特点:
1.入门前的小难题,初级课程。
2.适应于无需登录,明文但需要验证证书才能访问的页面。
3.获取的数据类型为HTML文档。
4.请求方法为Get/Post
3.第三招,根据URL地址获取需要登录才能访问的网页信息
我们先来分析一下这种类型的网页,需要登录才能访问的网页,其它呢也是一种验证,验证什么呢,验证客户端是否登录,是否具用相应的凭证,需要登录的都要验证SessionID这是每一个需要登录的页面都需要验证的,那我们怎么做的,我们第一步就是要得存在Cookie里面的数据包括SessionID,那怎么得到呢,这个方法很多,使用ID9或者是火狐浏览器很容易就能得到,可以参考我的文章
提供一个网页抓取hao123手机号码归属地的例子 这里面针对ID9有详细的说明。
如果我们得到了登录的Cookie信息之后那个再去访问相应的页面就会非常的简单了,其它说白了就是把本地的Cookie信息在请求的时候捎带过去就行了。
看代码
get方法
C# Code复制内容到剪贴板
- View Code
-
-
-
-
-
-
-
-
-
-
-
- public string GetUrltoHtml(string Url)
-
- {
-
- StringBuilder content = new StringBuilder();
-
- try
-
- {
-
-
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
-
- request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)";
-
- request.Method = "GET";
-
- request.Accept = "*/*";
-
-
-
- request.Referer = "http://sufei.cnblogs.com";
-
- CookieContainer objcok = new CookieContainer();
-
- objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("键", "值"));
-
- objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("键", "值"));
-
- objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("sidi_sessionid", "360A748941D055BEE8C960168C3D4233"));
-
- request.CookieContainer = objcok;
-
-
-
-
-
- request.KeepAlive = true;
-
-
-
-
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
-
-
-
-
- Stream responseStream = response.GetResponseStream();
-
-
-
-
-
- StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
-
-
-
-
-
- Char[] sReaderBuffer = new Char[256];
-
- int count = sReader.Read(sReaderBuffer, 0, 256);
-
- while (count > 0)
-
- {
-
- String tempStr = new String(sReaderBuffer, 0, count);
-
- content.Append(tempStr);
-
- count = sReader.Read(sReaderBuffer, 0, 256);
-
- }
-
-
-
- sReader.Close();
-
- }
-
- catch (Exception)
-
- {
-
- content = new StringBuilder("Runtime Error");
-
- }
-
-
-
- return content.ToString();
-
- }
post方法。
C# Code复制内容到剪贴板
- View Code
-
-
-
-
-
-
-
- public string OpenReadWithHttps(string URL, string strPostdata)
- {
- Encoding encoding = Encoding.Default;
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
- request.Method = "post";
- request.Accept = "text/html, application/xhtml+xml, */*";
- request.ContentType = "application/x-www-form-urlencoded";
- CookieContainer objcok = new CookieContainer();
- objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("键", "值"));
- objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("键", "值"));
- objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("sidi_sessionid", "360A748941D055BEE8C960168C3D4233"));
- request.CookieContainer = objcok;
-
- byte[] buffer = encoding.GetBytes(strPostdata);
- request.ContentLength = buffer.Length;
-
- request.GetRequestStream().Write(buffer, 0, buffer.Length);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
- return reader.ReadToEnd();
-
- }
特点:
1.还算有点水类型的,练习成功后可以小牛一把。
2.适应于需要登录才能访问的页面。
3.获取的数据类型为HTML文档。
4.请求方法为Get/Post
总结一下,其它基本的技能就这几个部分,如果再深入的话那就是基本技能的组合了
比如,
1. 先用Get或者Post方法登录然后取得Cookie再去访问页面得到信息,这种其它也是上面技能的组合,
这里需要以请求后做这样一步
response.Cookies
这就是在你请求后可以得到当次Cookie的方法,直接取得返回给上一个方法使用就行了,上面我们都是自己构造的,在这里直接使用这个Cookie就可以了。
2.如果我们碰到需要登录而且还要验证证书的网页怎么办,其它这个也很简单把我们上面的方法综合 一下就行了
如下代码这里我以Get为例子Post例子也是同样的方法
C# Code复制内容到剪贴板
- View Code
-
-
-
-
-
-
- public string GetUrltoHtml(string Url)
- {
- StringBuilder content = new StringBuilder();
-
- try
- {
-
- ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
-
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
-
-
- X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer");
-
-
- request.ClientCertificates.Add(objx509);
-
- CookieContainer objcok = new CookieContainer();
- objcok.Add(new Uri("http://www.cnblogs.com"), new Cookie("键", "值"));
- objcok.Add(new Uri("http://www.cnblogs.com"), new Cookie("键", "值"));
- objcok.Add(new Uri("http://www.cnblogs.com"), new Cookie("sidi_sessionid", "360A748941D055BEE8C960168C3D4233"));
- request.CookieContainer = objcok;
-
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
- Stream responseStream = response.GetResponseStream();
-
- StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
-
- Char[] sReaderBuffer = new Char[256];
- int count = sReader.Read(sReaderBuffer, 0, 256);
- while (count > 0)
- {
- String tempStr = new String(sReaderBuffer, 0, count);
- content.Append(tempStr);
- count = sReader.Read(sReaderBuffer, 0, 256);
- }
-
- sReader.Close();
- }
- catch (Exception)
- {
- content = new StringBuilder("Runtime Error");
- }
-
- return content.ToString();
-
- }
3.如果我们碰到那种需要验证网页来源的方法应该怎么办呢,这种情况其它是有些程序员会想到你可能会使用程序,自动来获取网页信息,为了防止就使用页面来源来验证,就是说只要不是从他们所在页面或是域名过来的请求就不接受,有的是直接验证来源的IP,这些都可以使用下面一句来进入,这主要是这个地址是可以直接伪造的
request.Referer = "http://sufei.cnblogs.com";
呵呵其它很简单因为这个地址可以直接修改。但是如果服务器上验证的是来源的URL那就完了,我们就得去修改数据包了,这个有点难度暂时不讨论。
4.提供一些与这个例子相配置的方法
过滤HTML标签的方法
C# Code复制内容到剪贴板
- View Code
-
-
-
-
-
-
- public static string StripHTML(string stringToStrip)
- {
-
- stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>", " ", RegexOptions.IgnoreCase | RegexOptions.Compiled);
- stringToStrip = Regex.Replace(stringToStrip, "<br(?:\\s*)/>", " ", RegexOptions.IgnoreCase | RegexOptions.Compiled);
- stringToStrip = Regex.Replace(stringToStrip, "\"", "‘‘", RegexOptions.IgnoreCase | RegexOptions.Compiled);
- stringToStrip = StripHtmlXmlTags(stringToStrip);
- return stringToStrip;
- }
-
- private static string StripHtmlXmlTags(string content)
- {
- return Regex.Replace(content, "<[^>]+>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
- }
URL转化的方法
C# Code复制内容到剪贴板
- #region 转化 URL
-
- public static string URLDecode(string text)
- {
- return HttpUtility.UrlDecode(text, Encoding.Default);
- }
-
- public static string URLEncode(string text)
- {
- return HttpUtility.UrlEncode(text, Encoding.Default);
- }
-
- #endregion
提供一个实际例子,这个是使用IP138来查询手机号码归属地的方法,其它在我的上一次文章里都有,在这里我再放上来是方便大家阅读,这方面的技术其它研究起来很有意思,希望大家多提建议,我相信应该还有更多更好,更完善的方法,在这里给大家提供一个参考吧。感谢支持
上例子
C# Code复制内容到剪贴板
-
-
-
-
- public static string[] getTelldate(string number)
- {
- try
- {
- string strSource = GetUrltoHtml("http://www.ip138.com:8080/search.asp?action=mobile&mobile=" + number.Trim());
-
- strSource = strSource.Substring(strSource.IndexOf(number));
- strSource = StripHTML(strSource);
- strSource = strSource.Replace(" ", "");
- strSource = strSource.Replace(" ", "");
- strSource = strSource.Replace("\t", "");
- strSource = strSource.Replace(" ", "");
- strSource = strSource.Replace("-->", "");
- string[] strnumber = strSource.Split(new string[] { "归属地", "卡类型", "邮 编", "区 号", "更详细", "卡号" }, StringSplitOptions.RemoveEmptyEntries);
- string[] strnumber1 = null;
- if (strnumber.Length > 4)
- {
- strnumber1 = new string[] { strnumber[1].Trim(), strnumber[2].Trim(), strnumber[3].Trim(), strnumber[4].Trim() };
- }
- return strnumber1;
- }
- catch (Exception)
- {
- return null;
- }
- }
这个例子写是不怎么样,些地方是可以简化的,这个接口而且可以直接使用Xml得到,但我在这里的重点是让一些新手看看方法和思路风凉啊,呵呵
第四招,通过Socket访问
C# Code复制内容到剪贴板
-
-
-
-
- private static string SMSrequest(string strSMSRequest)
- {
- byte[] data = new byte[1024];
- string stringData = null;
- IPHostEntry gist = Dns.GetHostByName("www.110.cn");
- IPAddress ip = gist.AddressList[0];
-
- IPEndPoint ipEnd = new IPEndPoint(ip, 3121);
-
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-
- try
- {
- socket.Connect(ipEnd);
- }
- catch (SocketException ex)
- {
- return "Fail to connect server " + ex.ToString();
- }
- string path = strSMSRequest.ToString().Trim();
- StringBuilder buf = new StringBuilder();
-
-
-
- byte[] ms = System.Text.UTF8Encoding.UTF8.GetBytes(buf.ToString());
-
- socket.Send(ms);
-
- string strSms = "";
- int recv = 0;
- do
- {
- recv = socket.Receive(data);
- stringData = Encoding.ASCII.GetString(data, 0, recv);
-
- strSms = strSms + stringData;
-
- }
- while (recv != 0);
- socket.Shutdown(SocketShutdown.Both);
- socket.Close();
- return strSms;
- }
c# HttpWebRequest与HttpWebResponse请求网页和返回网页教程
标签:
原文地址:http://www.cnblogs.com/xiangliqi/p/4384477.html