我需要做一个跨域传值登陆的问题。。A域上的一个页面通过http来post传值
代码如下
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "userCode=" + userCode;            byte[] data = encoding.GetBytes(postData);            // Prepare web request...    
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost:2867/Login.aspx");            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            
            Stream newStream = myRequest.GetRequestStream();            // Send the data.    
            newStream.Write(data, 0, data.Length);
            newStream.Close();
 
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
            string content = reader.ReadToEnd();
            if (content.Equals("True"))
            {
                Response.Redirect("http://localhost:2867/Main.aspx");
            }在B域的http://localhost:2867/Login.aspx页面中我接受post过来的值并设置cookie。
            Response.ContentType = "text/plain";
            string userCode = Request["userCode"];
            HttpCookie cookie = new HttpCookie("userCode", userCode);
            cookie.Expires = DateTime.Now.Date.AddDays(1);
            Response.Cookies.Add(cookie);
            Response.Write(true);最后跳转成功后在http://localhost:2867/Main.aspx中我想取刚设置的cookie,发现cookie是null,请问这是怎么回事。是不是httpResponse跳转后只能传值不能设置cookie。。有没有好的解决办法呢。?

解决方案 »

  1.   

    先在本地保存一个Cookie。用System.Net.Cookie类,好像。
      

  2.   

    CookieContainer cookie = new CookieContainer ();
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.AllowAutoRedirect = true;
                myRequest.CookieContainer = cookie;
                HttpWebResponse myresponse = (HttpWebResponse)myRequest.GetResponse();
                myresponse.Cookies = cookie.GetCookies(myRequest.RequestUri);            Stream mystream = null;
                mystream = myresponse.GetResponseStream();
                StreamReader myreader = new StreamReader(mystream, System.Text.Encoding.Default, true);
                string pagefile = myreader.ReadToEnd();
                  
                string postUrl = "";           
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(postUrl);
                Request.CookieContainer = cookie;