Windows phone 7 无法将cookie添加到从wp7中的第一个请求接收的httpwebrequest 从第一个请求收到的cookie: 无法在第二个请求中设置cookie

Windows phone 7 无法将cookie添加到从wp7中的第一个请求接收的httpwebrequest 从第一个请求收到的cookie: 无法在第二个请求中设置cookie,windows-phone-7,Windows Phone 7,请提供上述问题的解决方案…事实证明,您最好从第一个请求开始捕获cookie存储(像这样) 并在创建新请求时直接使用此cookie存储。(而不是像最初那样手动添加cookie) 注意-如果您传递的Cookie是HTTP ONLY,那么这实际上是处理这些Cookie的唯一方法(在当前版本的windows phone 7中) private void PostResponseCallback(IAsyncResult asyncResult) { try

请提供上述问题的解决方案…

事实证明,您最好从第一个请求开始捕获cookie存储(像这样)

并在创建新请求时直接使用此cookie存储。(而不是像最初那样手动添加cookie)

注意-如果您传递的Cookie是HTTP ONLY,那么这实际上是处理这些Cookie的唯一方法(在当前版本的windows phone 7中)

private void PostResponseCallback(IAsyncResult asyncResult)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                Stream content = response.GetResponseStream();
                SESSIONID = response.Headers["Set-cookie"].ToString();
                if (request != null && response != null)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (StreamReader reader = new StreamReader(content))
                        {
                            string _responseString = reader.ReadToEnd();
                            ResponseString = _responseString;
                            reader.Close();
                        }
                    }
                }
            }
public void AccDetialsGetResponse()
        {
            try
            {

                //CookieContainer cc1 = new CookieContainer();
                CookieCollection collection = new CookieCollection();
                SESSIONID = SESSIONID.Replace("; Path=/", "");
                Cookie cook = new Cookie();
                cook.Name = "cookie";
                cook.Value = SESSIONID;
                collection.Add(cook);

                //cc1.Add(new Uri(strHttpsUrl), cccs);
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strHttpsUrl);

                req.ContentType = "text/xml;charset=\"utf-8\"";
                req.Accept = "text/xml";
                req.Method = "POST";
                req.CookieContainer = new CookieContainer();
                req.CookieContainer.Add(new Uri(strHttpsUrl), collection);
                req.BeginGetRequestStream(AccDetailsPostRequest, req);
}
//keep in mind ResponseAndCookies is just a hand rolled obj I used to hold both cookies and the html returned during the response
private ResponseAndCookies ReadResponse(IAsyncResult result)
    {
        Stream dataStream = null;
        HttpWebRequest request = (HttpWebRequest) result.AsyncState;
        HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse;
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();   

        var responseAndCookies = new ResponseAndCookies
                                     {CookieContainer = request.CookieContainer, Markup = responseFromServer};

        return responseAndCookies;
    }
public void Checkout(ResponseAndCookies responseAndCookies)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/checkout");
        request.ContentType = "application/json";
        request.CookieContainer = responseAndCookies.CookieContainer; 
        request.Method = "POST";
        request.AllowAutoRedirect = false;
        request.Accept = "application/json";

        request.BeginGetRequestStream(new AsyncCallback(GetRequest), request);
    }