Chinaunix首页 | 论坛 | 博客
  • 博客访问: 593913
  • 博文数量: 64
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-09 20:38
文章存档

2012年(1)

2011年(8)

2010年(16)

2009年(18)

2008年(21)

我的朋友

分类:

2011-04-01 07:06:48

 #region 模拟POST登录,返回CookieCollection
       
///
       
/// POST数据之后返回cookies
       
///

        public CookieCollection GetCookie(string strUrl, string content, Encoding encoding, string referer)
        {

            HttpWebResponse response
= null;
            Stream responseStream
= null;
            StreamReader reader
= null;
            HttpWebRequest wr
= (HttpWebRequest)WebRequest.Create(strUrl);
            wr.Timeout
= 20000;
            wr.Method
= "POST";
            wr.UserAgent
= "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";
            wr.AllowAutoRedirect
= false;

            CookieContainer cc
= new CookieContainer();
            wr.CookieContainer
= cc;
           
try
            {
               
//将数据放到post流中
                byte[] data = Encoding.UTF8.GetBytes(content);
                wr.ContentType
= "application/x-www-form-urlencoded";
                wr.ContentLength
= data.Length;
                Stream reqData
= wr.GetRequestStream();
                reqData.Write(data,
0, data.Length);
                reqData.Close();

                response
= (HttpWebResponse)wr.GetResponse(); //创建一个合适的对象/ 2获得服务器上得到的数据
                responseStream = response.GetResponseStream(); //得到数据流
                reader = new StreamReader(responseStream, encoding);
               
string htmlContent = reader.ReadToEnd();

               
return response.Cookies;
            }
           
catch (Exception exn)
            {
            }
           
finally
            {
               
if (reader != null)
                {
                    reader.Close();
                }
               
if (responseStream != null)
                {
                    responseStream.Close();
                }
               
if (response != null)
                {
                    response.Close();
                }
            }
           
return new CookieCollection();
        }

 

通过应用程序读取网页信息的时候,通常需要抓取网页的数据,但是有一个问题就是很多网页需要登录后才能够获得页面数据,那么就需要保存当前的 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



 
阅读(468) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~