在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧。 1 public static string GetUrltoHtml(string Url,string type) 2 { 3 try 4 { 5 System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url); 6 // Get the response instance. 7 System.Net.WebResponse wResp = wReq.GetResponse(); 8 System.IO.Stream respStream = wResp.GetResponseStream(); 9 // Dim reader As StreamReader = New StreamReader(respStream) 10 using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type))) 11 { 12 return reader.ReadToEnd(); 13 } 14 } 15 catch (System.Exception ex) 16 { 17 //errorMsg = ex.Message; 18 } 19 return ""; 20 } post方法 1 ///<summary> 2 ///采用https协议访问网络 3 ///</summary> 4 ///<param name="URL">url地址</param> 5 ///<param name="strPostdata">发送的数据</param> 6 ///<returns></returns> 7 public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding) 8 { 9 Encoding encoding = Encoding.Default; 10 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); 11 request.Method = "post"; 12 request.Accept = "text/html, application/xhtml+xml, */*"; 13 request.ContentType = "application/x-www-form-urlencoded"; 14 byte[] buffer = encoding.GetBytes(strPostdata); 15 request.ContentLength = buffer.Length; 16 request.GetRequestStream().Write(buffer, 0, buffer.Length); 17 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 18 using( StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding))) 19 { 20 return reader.ReadToEnd(); 21 } 22 } 这招是入门第一式, 特点: get方法 1 //回调验证证书问题 2 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 3 { 4 // 总是接受 5 return true; 6 } 7 8 /// <summary> 9 /// 传入URL返回网页的html代码 10 /// </summary> 11 /// <param name="Url">URL</param> 12 /// <returns></returns> 13 public string GetUrltoHtml(string Url) 14 { 15 StringBuilder content = new StringBuilder(); 16 17 try 18 { 19 //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 20 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 21 22 // 与指定URL创建HTTP请求 23 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 24 25 //创建证书文件 26 X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer"); 27 28 //添加到请求里 29 request.ClientCertificates.Add(objx509); 30 31 // 获取对应HTTP请求的响应 32 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 33 // 获取响应流 34 Stream responseStream = response.GetResponseStream(); 35 // 对接响应流(以"GBK"字符集) 36 StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); 37 // 开始读取数据 38 Char[] sReaderBuffer = new Char[256]; 39 int count = sReader.Read(sReaderBuffer, 0, 256); 40 while (count > 0) 41 { 42 String tempStr = new String(sReaderBuffer, 0, count); 43 content.Append(tempStr); 44 count = sReader.Read(sReaderBuffer, 0, 256); 45 } 46 // 读取结束 47 sReader.Close(); 48 } 49 catch (Exception) 50 { 51 content = new StringBuilder("Runtime Error"); 52 } 53 54 return content.ToString(); 55 } post方法 1 //回调验证证书问题 2 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 3 { 4 // 总是接受 5 return true; 6 } 7 8 ///<summary> 9 ///采用https协议访问网络 10 ///</summary> 11 ///<param name="URL">url地址</param> 12 ///<param name="strPostdata">发送的数据</param> 13 ///<returns></returns> 14 public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding) 15 { 16 // 这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 17 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 18 Encoding encoding = Encoding.Default; 19 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); 20 21 //创建证书文件 22 X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer"); 23 24 //加载Cookie 25 request.CookieContainer = new CookieContainer(); 26 27 //添加到请求里 28 request.ClientCertificates.Add(objx509); 29 request.Method = "post"; 30 request.Accept = "text/html, application/xhtml+xml, */*"; 31 request.ContentType = "application/x-www-form-urlencoded"; 32 byte[] buffer = encoding.GetBytes(strPostdata); 33 request.ContentLength = buffer.Length; 34 request.GetRequestStream().Write(buffer, 0, buffer.Length); 35 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 36 using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding))) 37 { 38 return reader.ReadToEnd(); 39 } 40 } 这招是学会算是进了大门了,凡是需要验证证书才能进入的页面都可以使用这个方法进入,我使用的是证书回调验证的方式,证书验证是否通过在客户端验证,这样的话我们就可以使用自己定义一个方法来验证了,有的人会说那也不清楚是怎么样验证的啊,其它很简单,代码是自己写的为什么要那么难为自己呢,直接返回一个True不就完了,永远都是验证通过,这样就可以无视证书的存在了, 特点: 1 /// <summary> 2 /// 传入URL返回网页的html代码带有证书的方法 3 /// </summary> 4 /// <param name="Url">URL</param> 5 /// <returns></returns> 6 public string GetUrltoHtml(string Url) 7 { 8 StringBuilder content = new StringBuilder(); 9 try 10 { 11 // 与指定URL创建HTTP请求 12 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 13 request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)"; 14 request.Method = "GET"; 15 request.Accept = "*/*"; 16 //如果方法验证网页来源就加上这一句如果不验证那就可以不写了 17 request.Referer = "http://sufei.cnblogs.com"; 18 CookieContainer objcok = new CookieContainer(); 19 objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("键", "值")); 20 objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("键", "值")); 21 objcok.Add(new Uri("http://sufei.cnblogs.com"), new Cookie("sidi_sessionid", "360A748941D055BEE8C960168C3D4233")); 22 request.CookieContainer = objcok; 23 24 //不保持连接 25 request.KeepAlive = true; 26 27 // 获取对应HTTP请求的响应 28 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 29 30 // 获取响应流 31 Stream responseStream = response.GetResponseStream(); 32 33 // 对接响应流(以"GBK"字符集) 34 StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312")); 35 36 // 开始读取数据 37 Char[] sReaderBuffer = new Char[256]; 38 int count = sReader.Read(sReaderBuffer, 0, 256); 39 while (count > 0) 40 { 41 String tempStr = new String(sReaderBuffer, 0, count); 42 content.Append(tempStr); 43 count = sReader.Read(sReaderBuffer, 0, 256); 44 } 45 // 读取结束 46 sReader.Close(); 47 } 48 catch (Exception) 49 { 50 content = new StringBuilder("Runtime Error"); 51 } 52 53 return content.ToString(); 54 } post方法。 ///<summary> ///采用https协议访问网络 ///</summary> ///<param name="URL">url地址</param> ///<param name="strPostdata">发送的数据</param> ///<returns></returns> 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(); } 特点: [code=csharp]response.Cookies[/code]
这就是在你请求后可以得到当次Cookie的方法,直接取得返回给上一个方法使用就行了,上面我们都是自己构造的,在这里直接使用这个Cookie就可以了。 1 // <summary> 2 /// 传入URL返回网页的html代码 3 /// </summary> 4 /// <param name="Url">URL</param> 5 /// <returns></returns> 6 public string GetUrltoHtml(string Url) 7 { 8 StringBuilder content = new StringBuilder(); 9 10 try 11 { 12 //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 13 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 14 15 // 与指定URL创建HTTP请求 16 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 17 18 //创建证书文件 19 X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer"); 20 21 //添加到请求里 22 request.ClientCertificates.Add(objx509); 23 24 CookieContainer objcok = new CookieContainer(); 25 objcok.Add(new Uri("http://www.cnblogs.com"), new Cookie("键", "值")); 26 objcok.Add(new Uri("http://www.cnblogs.com"), new Cookie("键", "值")); 27 objcok.Add(new Uri("http://www.cnblogs.com"), new Cookie("sidi_sessionid", "360A748941D055BEE8C960168C3D4233")); 28 request.CookieContainer = objcok; 29 30 // 获取对应HTTP请求的响应 31 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 32 // 获取响应流 33 Stream responseStream = response.GetResponseStream(); 34 // 对接响应流(以"GBK"字符集) 35 StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); 36 // 开始读取数据 37 Char[] sReaderBuffer = new Char[256]; 38 int count = sReader.Read(sReaderBuffer, 0, 256); 39 while (count > 0) 40 { 41 String tempStr = new String(sReaderBuffer, 0, count); 42 content.Append(tempStr); 43 count = sReader.Read(sReaderBuffer, 0, 256); 44 } 45 // 读取结束 46 sReader.Close(); 47 } 48 catch (Exception) 49 { 50 content = new StringBuilder("Runtime Error"); 51 } 52 53 return content.ToString(); 54 55 } 3.如果我们碰到那种需要验证网页来源的方法应该怎么办呢,这种情况其它是有些程序员会想到你可能会使用程序,自动来获取网页信息,为了防止就使用页面来源来验证,就是说只要不是从他们所在页面或是域名过来的请求就不接受,有的是直接验证来源的IP,这些都可以使用下面一句来进入,这主要是这个地址是可以直接伪造的 代码: 呵呵其它很简单因为这个地址可以直接修改。但是如果服务器上验证的是来源的URL那就完了,我们就得去修改数据包了,这个有点难度暂时不讨论。 1 /// <summary> 2 /// 过滤html标签 3 /// </summary> 4 /// <param name="strHtml">html的内容</param> 5 /// <returns></returns> 6 public static string StripHTML(string stringToStrip) 7 { 8 // paring using RegEx // 9 stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>", "\n\n", RegexOptions.IgnoreCase | RegexOptions.Compiled); 10 stringToStrip = Regex.Replace(stringToStrip, " 11 ", "\n", RegexOptions.IgnoreCase | RegexOptions.Compiled); 12 stringToStrip = Regex.Replace(stringToStrip, "\"", "''", RegexOptions.IgnoreCase | RegexOptions.Compiled); 13 stringToStrip = StripHtmlXmlTags(stringToStrip); 14 return stringToStrip; 15 } 16 17 private static string StripHtmlXmlTags(string content) 18 { 19 return Regex.Replace(content, "<[^>]+>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled); 20 } 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); } 文章源码出自:http://www.sufeinet.com/thread-6-1-1.html |
请发表评论