0

我正在制作一个应用程序,以在特定时间间隔后通过网络异步发送我的 Windows 8 Phone 的电池电量。我正在使用DispatcherTimerfor call 在某个特定时间间隔后发送电池电量。

DispatcherTimer timer = new DispatcherTimer();      //Create the timer instance
        timer.Interval = TimeSpan.FromSeconds(15);          //Set the timer interval
        timer.Tick += SendAsyncRequest;                     //Call this procedure after the time ends

        timer.Start();  

我想使用 POST 请求发送我的电池电量。

void SendAsyncRequest(object sender, object e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.2.17/android_connect/add_device.php");            
        request.Method = "POST";
        request.BeginGetRequestStream(new AsyncCallback(SendBatteryLevel), request);

    }

现在,我发送实际请求 ( ) 的代码的核心SendBatteryLevel如下:

void SendBatteryLevel(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
        {
            string username = "CoolSops";
            string deviceId = "WindowsPhone";
            string batteryLevel = Windows.Phone.Devices.Power.Battery.GetDefault().RemainingChargePercent.ToString();
            string post_param = "username=" + username + "&device_id=" + deviceId + "&battery_level=" + batteryLevel;
            try
            {
                byte[] requestBody = System.Text.Encoding.UTF8.GetBytes(post_param);
                postStream.Write(requestBody, 0, requestBody.Length);
                SendingStatus.Text = "Battery data sent";
                postStream.Close();
            }
            catch (Exception e)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    ErrorStatus.Text = e.ToString();
                });                   
                Console.WriteLine(e.Message + "\n\n" + e.Source + "\n\n" + e.StackTrace);
            }
        }           
    }

在哪里SendingStatusErrorStatusTextBlocks。当我执行此代码时,我得到一个System.UnauthorizedAccessException. 异常详情如下:

A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

我不明白我试图以未经授权的方式访问什么。谁能说出为什么会这样?

注意:我在调试程序时注意到了一件有趣的事情。当从方法SendBatteryLevel调用方法SendAsyncRequest时,从控件执行几行后SendBatteryLevel返回SendAsyncRequest。我试图找出控制返回的确切行数,SendAsyncRequest但每次都不同。因此,在方法SendBatteryLevel完全执行之前,该方法会以这种方式被调用两次或有时三次。可能这就是导致异常发生的原因。

4

2 回答 2

0

可以以更简单的方式使用字节数组。在获取响应流之前
还需要设置。ContentLength

尝试这个:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.2.17/android_connect/add_device.php");
request.Method = "POST";

string username = "CoolSops";
string deviceId = "WindowsPhone";
string batteryLevel = Windows.Phone.Devices.Power.Battery.GetDefault().RemainingChargePercent.ToString();
string post_param = "username=" + username + "&device_id=" + deviceId + "&battery_level=" + batteryLevel;

byte[] requestBody = System.Text.Encoding.UTF8.GetBytes(post_param);

request.ContentLength = requestBody.Length;

request.BeginGetRequestStream(
    result =>
    {
        using (var postStream = request.EndGetRequestStream(result))
        {
            postStream.Write(requestBody, 0, requestBody.Length);
            postStream.Close();
        }

        request.BeginGetResponse(
            (response) =>
            {
                // Handle response here....
            },
            null);
    },
request);
于 2013-03-07T16:42:58.337 回答
0

您是否在 WMAppManifest.xml 中设置了所有必要的功能?

在我看来,您忘记启用 ID_CAP_IDENTITY_DEVICE、ID_CAP_IDENTITY_USER 或 ID_CAP_NETWORKING。

你从哪里得到异常?你能得到电池状态吗?

于 2013-03-11T12:59:53.013 回答