开发者

Connect to Google Analytics from Silverlight

开发者 https://www.devze.com 2023-02-17 04:09 出处:网络
I created a simple SL 4 app to connect to Google Analytics but I keep getting the following exception:

I created a simple SL 4 app to connect to Google Analytics but I keep getting the following exception:

{System.Security.SecurityException: Security error.
   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at GoogleAnalytics.Silverlight.MainPage.<>c__DisplayClass4.<GetResponseCallback>b__3()}

I think it has something to do with xss but I'm not sure how to get around that. The following code works fine in a console app but doesn't in SL.

The UI has a textblock control, here's the code:

private byte[] _data;

public MainPage()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    string requestData = "Email=email@gmail.com" +
                         "&Passwd=password" +
                         "&source=Test-App" +
                         "&accountType=GOOGLE" +
                         "&service=analytics";

    _data = Encoding.UTF8.GetBytes(requestData);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://www.google.com/accounts/ClientLogin", UriKind.Absolute));
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = _data.Length;

    request.BeginGetRequestStream(GetRequestStreamCallback, request);
}

private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    this.Dispatcher.BeginInvoke(() =>
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            Stream stream = request.EndGetRequestStream(asynchronousResult);
            stream.Write(_data, 0, _data.Length);
            stream.Close();

            request.BeginGetResponse(GetResponseCallback, request);
        }
        catch (Exception e)
        {
            textBlock.Text = e.ToString();
        }
    });
}

private void GetResponseCallback(IAs开发者_开发问答yncResult asynchronousResult)
{
    this.Dispatcher.BeginInvoke(() =>
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);

            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);

            textBlock.Text = streamReader.ReadToEnd().Split(new string[] { "Auth=" }, StringSplitOptions.None)[1];

            streamReader.Close();
            response.Close();
        }
        catch (Exception e)
        {
            textBlock.Text = e.ToString();
        }
    });
}

Update: fixed a typo.

Thanks


Just some tipps, i haven't tested your code yet, but they might help.

  1. Use a WebClient for Web requests, it's much less and cleaner code... You will find examples on MSDN <- this link also contains almost all interesting topics on Silverlight Web/http/https requests and security - a must read in your scenario

  2. Problems might occur if you're testing from a wrong environment like testing the Silverlight object from a local file url (web Url starting with file:///...). Create a Web-Project in your service, configure the Silverlight Project to run in that created Web project under the Test Web Server or the local IIS

I know, no code examples just links or tipps, they might help you though.

Best regards

UPDATE

What exactly are you trying to do? If you want to track, I've seen a few ready solutions for silverlight, for example Silverlight Analytics or Tracking Silverlight. Here is another known solution.


Silverlight cannot connect to GA services, I either have to use a service or JavaScript then pass the data to Silverlight.

0

精彩评论

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