日期:2014-05-17  浏览次数:20931 次

有关cookies值的获取问题
 客户端访问webservice代码如下:
string cookies = string.Empty;
            HttpCookie cokie = new HttpCookie("MyCook");//初使化
            if (HttpContext.Current.Request.Cookies["MyCook"] != null)
            {  cookies = HttpContext.Current.Request.Cookies["MyCook"].Value;            }
            string url = "http://localhost/INA_WebService/Service.asmx/ImportStyle";
 string sendData = "param="+a;
            string outputString = String.Empty;
            HttpWebRequest hwrq = (HttpWebRequest)HttpWebRequest.Create(url);
            hwrq.Method = "POST";
            byte[] postData = System.Text.Encoding.UTF8.GetBytes(sendData);
            hwrq.ContentType = "application/x-www-form-urlencoded";
            hwrq.ContentLength = postData.Length;
            Stream writeStream = hwrq.GetRequestStream();
            writeStream.Write(postData, 0, postData.Length);
            writeStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)hwrq.GetResponse();
            StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            outputString = sr.ReadToEnd();
            sr.Close();
访问的webservice代码如下:
[WebMethod]    
   public bool ImportStyle()
    {
        string userName=DataOperation.GetCookies();
        bool strbool = false    ;
        if (userName != "")
        {
strbool = true;    
}
else
{strbool = false;}
return strbool;

GetCookies()代码如下:
  public static string GetCookies()
    {
        string cookies=string.Empty ;
         HttpCookie cokie = new HttpCookie("MyCook");//初使化
         if (HttpContext.Current.Request.Cookies["MyCook"] != null)
    { cookies = HttpContext.Current.Request.Cookies["MyCook"].Value;//输出全部的值
    }
        return cookies ;
    }

问题: 调试发现红色字体的cookies值是存在的,但是红色字体的userName值为空,客户端返回的值是false。红色字体的cookies