京东价格监控软件开发技术探讨二:通过HttpWebRequest获取指定网页数据
C#提供了HttpWebRequest对象来获取指定网页的数据,具体用法如下:
/// <summary>
/// 获取网页数据
/// </summary>
/// <param name="url"></param>
/// <param name="method">"POST" or "GET"</param>
/// <param name="data">when the method is "POST", the data will send to web server, if the method is "GET", the data should be string.empty</param>
/// <returns></returns>
public static string GetResponse(string url, string method, string data)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = true;
req.Method = method.ToUpper();
req.AllowAutoRedirect = true;
req.CookieContainer = CookieContainers;
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = IE7;
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Timeout = 50000;
if (method.ToUpper() == "POST" && data != null)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postBytes = encoding.GetBytes(data); ;
req.ContentLength = postBytes.Length;
Stream st = req.GetRequestStream();
st.Write(postBytes, 0, postBytes.Length);
st.Close();
}
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};
//Encoding myEncoding = Encoding.GetEncoding("UTF-8");
Encoding myEncoding = Encoding.GetEncoding("gb2312");
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst, myEncoding);
string str = sr.ReadToEnd();
return str;
}
catch (Exception)
{
return string.Empty;
}
}
杀京东 做最走心的京东价格监控软件 免费开源的价格监控软件 欢迎加入QQ群415014949一起讨论