在做一个东西,从一个页面获取信息,但是要两次访问,第一个访问保存cookie,第二次访问才能获取正确的信息。我按照网上的代码,自己也改了挺长时间,但是都没有获取正确的信息。
代码如下: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strLoginURL);
            CookieContainer objcok = new CookieContainer();
            request.CookieContainer = objcok;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            objcok.Add(response.Cookies);
            //request = (HttpWebRequest)WebRequest.Create(strLoginURL);
            response = (HttpWebResponse)request.GetResponse();
            // 获取响应流
            Stream responseStream = response.GetResponseStream();
            // 对接响应流(以"GBK"字符集)
            StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            // 开始读取数据
            Char[] sReaderBuffer = new Char[256];
            int count = sReader.Read(sReaderBuffer, 0, 256);
            while (count > 0)
            {
                String tempStr = new String(sReaderBuffer, 0, count);
                content.Append(tempStr);
                count = sReader.Read(sReaderBuffer, 0, 256);
            }
            // 读取结束
            sReader.Close();能获取到token字段即可,请各位不吝赐教。
返回的token字段一直是什么第一和第二个参数要是string,反正是不正确的字段。URL为https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=falseCookieHttpWebRequest

解决方案 »

  1.   

    可以通过Request对象的 Cookie 集合来获取了,如:
    Request.Cookies[varName].Value
    你的代码中都没有获取cookie的代码的,详细内容参考下面两篇博文:
    http://www.cnblogs.com/shengchanlix/archive/2011/08/29/2158987.html
    http://www.cnblogs.com/freeliver54/archive/2013/04/09/3011059.html
      

  2.   

    你要取到token就可以了吗?如果是那样的话,我从浏览器直接就可以看到这个发出请求之后返回的是一个字符串,你可以使用WebClient.DownloadString(url)方法来获得回复的字符串,然后再对返回的字符串进行操作就可以了,直接找到token的字符位置,然后在从后面读取指定位数就可以获取token的值了,感觉根本不是操作cookies的来实现的,感觉思路就错了,我感觉cookies这个集合中应该不会有你想要的token这个值的,你可以单步调试看看Request.Cookies这个集合是否有你所要数据,如果没有的话,思路就错了,应该按照我说的思路来实现,即对返回数据进行操作,这时候就转变为简单的字符串操作了。
      

  3.   

    WebClient.DownloadString——http://msdn.microsoft.com/zh-cn/library/fhd1f0sw.aspx
      

  4.   

    是取token没错,如果用浏览器,删掉该网页的cookie后,刷新,第一次返回的是有问题的数据,然后再刷新一次就会返回正确的数据,我现在就是要模拟这个过程,没有cookie他会首先保存cookie,保存完之后再发送一个get,才会得到正确的数据。
      

  5.   

     string strLoginURL = "https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=false";
                string cookiesStr;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strLoginURL);
                request.CookieContainer = new CookieContainer();
                //取cookies
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    var cookies = response.Cookies;
                    //关键是这条,至于cookiesStr为什么要这样你可以通过ie(开发人员工具分析)
                    cookiesStr = string.Format("{0}={1};{2}={3}", cookies[1].Name, cookies[1].Value, cookies[0].Name, cookies[0].Value);
                }
                request = (HttpWebRequest)WebRequest.Create(strLoginURL);
                //关键是这条,至于cookiesStr为什么要这样你可以通过ie(开发人员工具分析)
                request.Headers.Add("Cookie", cookiesStr);
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    // 获取响应流
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                        //带你要求的bdPass.api.params.login_token
                        richTextBox1.AppendText(sReader.ReadToEnd());
                        // 读取结束
                        sReader.Close();
                    }
                }
    你的cookie CookieContainer  的形式发 过去不成功(估计cookie索引颠倒引起的)你可以用字符串发过去呀,像这样的例子最好用IE  的开发人员工具(F12)分析分析,或者用别的封包分析软件分析一下再提交
      

  6.   

    是cookie没有附加到第二次的request中,后来自己出来了,已经OK了,谢谢各位。