开发者

unable to add cookie to httpwebrequest recieved from the first request in wp7

开发者 https://www.devze.com 2023-03-10 09:20 出处:网络
cookie recieved from first request: private void PostResponseCallback(IAsyncResult asyncResult) { try { HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;

cookie recieved from first request:

private void PostResponseCallback(IAsyncResult asyncResult)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                Stream content = response.GetResponseStream();
      开发者_如何学C          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();
                        }
                    }
                }
            }

Unable to set cookie in second request

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

Kindly provide a solution to the above issue...


It turns out you are better off capturing the cookie store from the first request (like so)

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

And using this cookie store directly when you create then new request. (instead of manually adding the cookie like you had initially)

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

Note- if the cookies you are passing around are HTTP ONLY this is actually the ONLY way to deal with them (in the current release of windows phone 7)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号