无法将cookie添加到从wp7中的第一个请求收到的httpwebrequest

时间:2011-06-02 11:12:03

标签: windows-phone-7

从第一次请求收到的

cookie:

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();
                        }
                    }
                }
            }

无法在第二个请求中设置cookie

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);
}

请为上述问题提供解决方案......

1 个答案:

答案 0 :(得分:0)

事实证明你最好从第一个请求中捕获cookie存储(如此)

//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;
    }

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

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);
    }

注意 - 如果你传递的cookie只是HTTP,这实际上是处理它们的唯一方法(在当前版本的windows phone 7中)