我用httpwebrequest 下载 "http://www2.baidu.com/?castk=e0d54av7eb5470266d589"
本来想得到通过HttpWebResponse.Headers.Get("Set-cookie");得到返回的cookie但是奇怪的是,在下载这个页面的时候,会自动下载以下两个url
http://fengchao.baidu.com/indexAction.do?uid=448186
http://fengchao.baidu.com/indexAction.do?uid=448186&userid=448186结果通过HttpWebResponse.Headers.Get("Set-cookie");得到的是
http://fengchao.baidu.com/indexAction.do?uid=448186&userid=448186
这个url返回的cookie,而不是http://www2.baidu.com/?castk=e0d54av7eb5470266d589
这个url返回的cookie;我该怎么解决才能得到http://www2.baidu.com/?castk=e0d54av7eb5470266d589
这个url返回的set-cookie参数各位大虾帮帮忙!!!小弟很急

解决方案 »

  1.   

    源码如下:        public static string GetCookie(string URL,string curcookie, out string cookie)
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);            CookieContainer co = new CookieContainer();
                co.SetCookies(new Uri(URL), curcookie);
                myHttpWebRequest.CookieContainer = co;
                   
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                Stream receiveStream = myHttpWebResponse.GetResponseStream();
                Encoding encode = System.Text.Encoding.Default;
                StreamReader readStream = new StreamReader(receiveStream, encode);
                Char[] read = new Char[1024];    
                int count = readStream.Read( read, 0, 1024 );
                StringBuilder sb=new StringBuilder();
                while (count > 0) 
                {
                    String str = new String(read, 0, count);
                    sb.Append(str);
                    count = readStream.Read(read, 0, 1024);
                }
                //string header=myHttpWebResponse.Headers.ToString();
                cookie = myHttpWebResponse.Headers.Get("Set-cookie");
                myHttpWebResponse.Close();
                readStream.Close();
                return sb.ToString();        }
      

  2.   

    查看了数据包后发现是因为
    下载 "http://www2.baidu.com/?castk=e0d54av7eb5470266d589" 的时候,url有重定向,那么如何在重定向之前得到“set-cookie”值
      

  3.   

    查看了数据包后发现是因为
    下载 "http://www2.baidu.com/?castk=e0d54av7eb5470266d589" 的时候,url有重定向,那么如何在重定向之前得到“set-cookie”值
      

  4.   

    我是这样获取的,这样cookie变量中就能完全取得了public string GetHtml(string url, byte[] data, Encoding encoding)
            {
                HttpWebResponse res = null;
                string strResult = "";            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                try
                {
                    req.KeepAlive = true;
                    req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
                    req.Referer = referer;
                    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
                    //设置cookie
                    reqcookie.Add(cookie);
                    req.CookieContainer = reqcookie;
                    //req.CookieContainer.Add(cookie);                if (proxy != "")
                    {
                        WebProxy proxys = new WebProxy();
                        proxys.Address = new Uri("http://" + this.proxy);
                        req.Proxy = proxys;
                    }
                    else
                    {
                        req.Proxy = null;
                    }                //byte[] data = encoding.GetBytes(postdata);
                    if (data.Length > 0)
                    {
                        req.Method = "POST";
                        req.ContentType = "application/x-www-form-urlencoded";
                        req.ContentLength = data.Length;
                        using (System.IO.Stream sw = req.GetRequestStream())
                        {
                            try
                            {                            sw.Write(data, 0, data.Length);
                            }
                            catch
                            {
                                strResult = "";
                                return strResult;
                            }
                            finally
                            {                        }
                        }
                    }
                    try
                    {
                        HttpWebResponse response = null;
                        System.IO.StreamReader sr = null;                    try
                        {                        response = (HttpWebResponse)req.GetResponse();
                            foreach (Cookie c in response.Cookies)
                            {
                                Console.WriteLine("Name: {0}", c.Name);
                                Console.WriteLine("Value: {0}", c.Value);
                                Console.WriteLine("Domain: {0}", c.Domain);
                                Console.WriteLine("Expires: {0}", c.Expires);
                            }
                            Console.WriteLine("============================================");
                            this.cookie.Add(response.Cookies);
                            sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);                        strResult = sr.ReadToEnd();                    }
                        catch (WebException webex)
                        {
                            if (webex.Status == WebExceptionStatus.KeepAliveFailure)
                            {
                                strResult = "";
                                return strResult;
                            }
                            else
                            {
                                strResult = "";
                                return strResult;
                            }
                        }
                        catch (System.Exception ex)
                        {
                            strResult = ex.Message;
                            return strResult;
                        }
                        finally
                        {
                            if (sr != null) { sr.Close(); }
                            if (response != null) { response.Close(); }
                            response = null;
                            req = null;
                        }
                        return strResult;
                    }
                    catch
                    {
                        return "";
                    }
                }
                catch
                {
                    return "";
                }
            }
      

  5.   

    不好意思,没有看清楚你的问题,你这样的情况最好不要用HttpWebResponse,因为HttpWebResponse会自动重定向,你要获取的是第一个页面的数据,用TcpClient就可以了,用TcpClient发送HTTP包就可以用返回的数据中从取得Cookie的数据,
    模拟发送的包如下
    GET /?action=logout&u=http%3A%2F%2Fcas.baidu.com%2F%3Ftpl%3Dwww2 HTTP/1.1Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)Host: cas.baidu.com返回的数据包如下
    HTTP/1.1 302 FoundDate: Sat, 18 Jul 2009 03:13:09 GMTServer: Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7a DAV/2 PHP/5.2.4X-Powered-By: PHP/5.2.4Expires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cacheLocation: http://cas.baidu.com/?tpl=www2Content-Length: 0Connection: closeContent-Type: text/html
      

  6.   

    或者使HttpWebRequest不重定向就可以了
    请求.AllowAutoRedirect   =   false;