2013-03-07 16 views
0

我正在製作一個應用程序,在特定的時間間隔後通過網絡異步發送Windows 8 Phone的電池電量。我正在使用DispatcherTimer呼叫在特定時間間隔後發送電池電量。在什麼情況下你會得到`System.UnauthorizedAccessException`?

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); 
      } 
     }   
    } 

其中SendingStatusErrorStatusTextBlock秒。當我執行此代碼時,我得到一個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 

我不明白我試圖訪問未經授權的方式。任何人都可以告訴爲什麼發生這種情況?

注意: 我在調試程序時注意到了一個有趣的事情。當從方法SendAsyncRequest調用方法SendBatteryLevel時,在從SendBatteryLevel執行幾行後,控制返回到SendAsyncRequest。我試圖找出確切的線數,之後控制回到SendAsyncRequest,但每次都會變化。因此,方法SendBatteryLevel在方法完全執行之前以這種方式調用兩次或有時三次。可能是這是什麼導致發生異常。

+1

我錯過了什麼,或者你忘了描述_unable來做這件事嗎? – 2013-03-07 04:51:00

+0

我無法將電池電量發送到我的網絡應用程序。 – TheRookierLearner 2013-03-07 04:54:18

+0

是的,你的問題是這樣說的。但是......您能否詳細說明您遇到的特定問題,使您可以發送電池電量_? – 2013-03-07 04:58:29

回答

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); 
+0

我試過這個,但我仍然得到以下例外: '線程0xf34退出代碼259(0x103)。 mscorlib.ni.dll中發生類型爲「System.IO.FileNotFoundException」的異常,並且未在託管/本地邊界之前處理 System.Windows.ni中發生第一次機會例外,即System.UnauthorizedAccessException。 dll 在System.Windows.ni.dll中發生類型'System.UnauthorizedAccessException'的異常,並且在託管/本地邊界之前未處理該異常。 – TheRookierLearner 2013-03-07 21:30:37

+0

@DestinyCalling嘗試此更新的版本 – 2013-03-11 18:39:39

0

你有沒有設置在WMAppManifest.xml所有必要的能力?

它讓我覺得你忘了啓用ID_CAP_IDENTITY_DEVICE,ID_CAP_IDENTITY_USER或ID_CAP_NETWORKING。

你從哪裏得到例外?你能否獲得電池狀態?

+0

我設置了所有功能。我正在獲取電池電量。我無法通過網絡發送。 – TheRookierLearner 2013-03-11 16:53:20

相關問題