using System;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using System.IO;
using Microsoft.Phone.Net.NetworkInformation;

namespace PhoneAppWebRequest
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            WebRequest wrequest = WebRequest.Create("http://www.bing.com");
            //wrequest.SetNetworkPreference(NetworkSelectionCharacteristics.Cellular);
            //wrequest.SetNetworkRequirement(NetworkSelectionCharacteristics.Cellular);
            if (wrequest.GetCurrentNetworkInterface().InterfaceSubtype == NetworkInterfaceSubType.Cellular_3G)
            {
                wrequest.BeginGetResponse(new AsyncCallback(ResponseFinished), wrequest);
            }
        }

        void ResponseFinished(IAsyncResult result)
        {
            if (result.IsCompleted)
            {
                WebRequest wrequest = (WebRequest)result.AsyncState;
                try
                {
                    WebResponse wresponse = wrequest.EndGetResponse(result);
                    Dispatcher.BeginInvoke(() =>
                    {
                        using (StreamReader sr = new StreamReader(wresponse.GetResponseStream()))
                        {
                            MessageBox.Show(sr.ReadToEnd());
                            sr.Close();
                        }
                    });

                }
                catch (WebException webe)
                {

                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("Aufruf des WebRequests nicht möglich:\r\n"+webe.Message);
                    });

                }
            }
        }
    }
}