问题描述
这是我写的一段代码,获取到的cookie不对,求大神帮忙指点一下classProgram{publicstaticCookieContainertheCC=newCookieContainer();publicstaticstringLogin(){stringusername="";stringpassword="";stringpostdata="userName="+username+"&password="+password;HttpWebResponseres=null;HttpWebRequestreq=null;stringstrResult="";try{req=(HttpWebRequest)WebRequest.Create("http://tieba.baidu.com/");//配置请求headerreq.Headers.Add(HttpRequestHeader.AcceptCharset,"GBK,utf-8;q=0.7,*;q=0.3");req.Headers.Add(HttpRequestHeader.AcceptEncoding,"gzip,deflate,sdch");req.Headers.Add(HttpRequestHeader.AcceptLanguage,"zh-CN,zh;q=0.8");req.Accept="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";req.KeepAlive=true;req.Timeout=20000;req.Referer="http://tieba.baidu.com/";//req.Headers.Add(HttpRequestHeader.CacheControl,"max-age=0");req.UserAgent="Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/49.0.2623.110Safari/537.36";req.Method="POST";req.ContentType="text/html;charset=GBK";req.AllowAutoRedirect=true;//设置cookieContainer用来接收cookiereq.CookieContainer=theCC;//对参数进行encodebyte[]data=null;data=Encoding.UTF8.GetBytes(postdata);req.ContentLength=postdata.Length;StreamnewStream=req.GetRequestStream();newStream.Write(data,0,data.Length);newStream.Close();res=(HttpWebResponse)req.GetResponse();StreamReadersr=null;try{sr=newStreamReader(res.GetResponseStream(),Encoding.GetEncoding("gb2312"));strResult=sr.ReadToEnd();//sr.Close();if(strResult!=""&&!strResult.Contains("userName")){res.Close();req.Abort();Console.WriteLine("账号"+username+"登陆成功");stringcookies=theCC.GetCookieHeader(res.ResponseUri);Console.WriteLine(cookies);//HttpContext.Current.Application.Lock();//HttpContext.Current.Application["cookieHeader"]=cookies;//HttpContext.Current.Application.UnLock();}}catch(Exceptiona){a.StackTrace.ToString();Console.WriteLine(a.Message);}}catch(Exceptione){e.StackTrace.ToString();Console.WriteLine(e.Message);//writeLog}finally{if(res!=null){res.Close();}if(req!=null){req.Abort();}}return"";}///<summary>获取页面HTML//////<PARAMname="url">获取的页面地址</PARAM>///<RETURNS></RETURNS>publicstaticstringgetPage(Stringurl,stringreferer){HttpWebRequestreq=(HttpWebRequest)WebRequest.Create(url);stringstrResult=string.Empty;//req.Headers["If-None-Match"]="36d0ed736e88c71:d9f";req.Method="GET";req.Timeout=20000;req.Referer=referer;req.CookieContainer=theCC;HttpWebResponseres=(HttpWebResponse)req.GetResponse();StreamReadersr=null;try{sr=newStreamReader(res.GetResponseStream(),Encoding.Default);strResult=sr.ReadToEnd();if(strResult!=null){Console.WriteLine("捕获成功");}}catch(System.Exceptionex){ex.StackTrace.ToString();Console.WriteLine(ex.Message);//writeLog}finally{sr.Close();}returnstrResult;}staticvoidMain(string[]args){Console.WriteLine(Login());Console.WriteLine(Program.getPage("http://tieba.baidu.com/home/main?id=a4d864756c696e6c696ecaa8d7d35e41&fr=userbar","http://tieba.baidu.com/home/main?id=a4d864756c696e6c696ecaa8d7d35e41&fr=userbar"));Console.ReadLine();////Console.ReadLine();}}