Cookie没有进入webrequest

时间:2013-06-03 08:29:29

标签: c# wpf windows-phone-7.1

下面提到的代码是从登录URL获取cookie,我正在尝试登录并保存cookie以发送它以进行进一步的交互,但我没有得到任何可以传递的cookie。请帮忙

//我的登录方法::

 void login()

   {

        // Create the web request object
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Constants.LOGIN_URL);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.CookieContainer = cookieContainer;
        // Start the request
        webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
    }

    void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the stream request operation
        Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);


        // Create the post data
        string postData = "client_key=" + Constants.CLIENT_KEY + "&email=" + username + "&password=" + password;

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Add the post data to the web request
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        // Start the web request
        webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);

    }

    void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            //webRequest.CookieContainer = cookieContainer;
            //webRequest.Method = "GET";
            HttpWebResponse response;
            //Constants.userProfile.cookie = webRequest.CookieContainer;
            // End the get response operation

            response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

            foreach (Cookie cookieValue in response.Cookies)
            {
                Console.WriteLine("Cookie: " + cookieValue.ToString());
            }



            Stream streamResponse = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamResponse);
            var Response = streamReader.ReadToEnd();
            streamResponse.Close();
            streamReader.Close();
            response.Close();
            // Overlay.Visibility = System.Windows.Visibility.Collapsed;

        }
        catch (WebException e)
        {
            Dispatcher.BeginInvoke(() =>
                MessageBox.Show("Please try again", "Login Error", MessageBoxButton.OK));
            Dispatcher.BeginInvoke(() =>
                Console.WriteLine("Error Occurred" + e.Message));
        }
    }

0 个答案:

没有答案