none
Silverlight FAQ: Bagaimana saya bisa menangani isu yang terkait dengan Out-Of-Browser? (Part 3) RRS feed

  • Diskusi Umum

  • Namun, ketika aplikasi bekerja pada mode OOB, pendekatan di atas tidak bekerja lagi. Sebagai solusi, kita dapat menyimpan data dalam IsolatedStorageFile. Misalnya,

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Net;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Animation;

    using System.Windows.Shapes;

    using System.Net.Browser;

    using System.IO;

    using System.Text;

    using System.IO.IsolatedStorage;

    namespace CookiesEx

    {

        public partial class MainPage : UserControl

        {

            public MainPage()

            {

                InitializeComponent();

            }

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                InitializeWebRequestClientStackForURI();

                ReadFromIsolatedStorage();

            }

            private void InitializeWebRequestClientStackForURI()

            {

                // Create the client WebRequest creator.

                IWebRequestCreate creator = WebRequestCreator.ClientHttp;

                // Register both http and https.

                WebRequest.RegisterPrefix("http://", creator);

                WebRequest.RegisterPrefix("https://", creator);

                // Create a HttpWebRequest.

                HttpWebRequest request = (HttpWebRequest)

                    WebRequest.Create("http://api.search.live.net/clientaccesspolicy.xml");

                //Create the cookie container and add a cookie.

                request.CookieContainer = new CookieContainer();

                // This example shows manually adding a cookie, but you would most

                // likely read the cookies from isolated storage.

                request.CookieContainer.Add(new Uri("http://api.search.live.net"),

                    new Cookie("id", "1234"));

                // Send the request.

                request.BeginGetResponse(new AsyncCallback(ReadCallback), request);

            }

            // Get the response and write cookies to isolated storage.

            private void ReadCallback(IAsyncResult asynchronousResult)

            {

                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

                HttpWebResponse response = (HttpWebResponse)

                    request.EndGetResponse(asynchronousResult);

                using (IsolatedStorageFile isf =

                    IsolatedStorageFile.GetUserStoreForSite())

                {

                    using (IsolatedStorageFileStream isfs = isf.OpenFile("CookieExCookies",

                        FileMode.OpenOrCreate, FileAccess.Write))

                    {

                        using (StreamWriter sw = new StreamWriter(isfs))

                        {

                            foreach (Cookie cookieValue in response.Cookies)

                            {

                                sw.WriteLine("Cookie: " + cookieValue.ToString());

                            }

                            sw.Close();

                        }

                    }

                }

            }

            private void ReadFromIsolatedStorage()

            {

                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())

                {

                    using (IsolatedStorageFileStream isfs =

                       isf.OpenFile("CookieExCookies", FileMode.Open))

                    {

                        using (StreamReader sr = new StreamReader(isfs))

                        {

                            tb1.Text = sr.ReadToEnd();

                            sr.Close();

                        }

                    }

                }

            }

        }

    }

    Link terkait

    http://msdn.microsoft.com/en-us/library/dd920298(VS.95).aspx

    http://msdn.microsoft.com/en-us/library/system.web.httpcookie.httponly.aspx


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Kamis, 09 Juni 2011 05.42
    Moderator