全部博文(64)
分类:
2011-04-01 07:06:48
通过应用程序读取网页信息的时候,通常需要抓取网页的数据,但是有一个问题就是很多网页需要登录后才能够获得页面数据,那么就需要保存当前的 cookie,在.NET中可以使用CookieContainer 对象来保存登录后的Cookie信息,每次发送数据的时候加上Cookie信息,就可以解决这个问题了。
#region 同步通过POST方式发送数据
///
/// 通过POST方式发送数据
///
/// url
/// Post数据
/// Cookie容器
///
public string SendDataByPost(string Url,string postDataStr,ref CookieContainer cookie)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (cookie.Count == 0)
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}request.Method = “POST”;
request.ContentType = “application/x-www-form-urlencoded”;
request.ContentLength = postDataStr.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding(“gb2312″));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(“utf-8″));
string retString= myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();return retString;
}
#endregion#region 同步通过GET方式发送数据
///
/// 通过GET方式发送数据
///
/// url
/// GET数据
/// GET容器
///
public string SendDataByGET(string Url, string postDataStr, ref CookieContainer cookie)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url +( postDataStr == “” ? “” : “?”) + postDataStr);
if (cookie.Count == 0)
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}request.Method = “GET”;
request.ContentType = “text/html;charset=UTF-8″;HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(“utf-8″));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();return retString;
}
#endregion