C#抓取远程页面内容,以POST|GET方式抓取远程页面内容
以GET方式抓取远程页面内容
以POST方式抓取远程页面内容
using System; using System.Data; using System.Web.UI; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions;//包含必要的库 namespace Utils { /// <summary> /// 抓取远程页面内容 /// </summary> public static class HttpHelp { //以GET方式抓取远程页面内容 public static string Get_Http(string tUrl) { string strResult; try { HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(tUrl); hwr.Timeout = 19600; HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); Stream myStream = hwrs.GetResponseStream(); StreamReader sr = new StreamReader(myStream, Encoding.Default); StringBuilder sb = new StringBuilder(); while (-1 != sr.Peek()) { sb.Append(sr.ReadLine() "\r\n"); } strResult = sb.ToString(); hwrs.Close(); } catch (Exception ee) { strResult = ee.Message; } return strResult; } //以POST方式抓取远程页面内容 //postData为参数列表 public static string Post_Http(string url, string postData, string encodeType) { string strResult = null; try { Encoding encoding = Encoding.GetEncoding(encodeType); byte[] POST = encoding.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = POST.Length; Stream newStream = myRequest.GetRequestStream(); newStream.Write(POST, 0, POST.Length); //设置POST newStream.Close(); // 获取结果数据 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default); strResult = reader.ReadToEnd(); } catch (Exception ex) { strResult = ex.Message; } return strResult; } } }
原文链接:C#原生实现POST|GET方式发送Http请求帮助类