2014-02-05 51 views
-1

我正在开发wp8应用程序。我的问题是,当我点击注册按钮时,该应用程序出现错误,并且发生以下异常,并且代码不在字符串发布数据之后如何将我的数据发布到服务器上?

发生了类型System.UnauthorizedAccessException的例外System.Windows.ni .DLL但是如果有这种异常的处理程序是在用户代码

处理,该程序可以安全地继续

namespace docapp 
{ 
    public partial class registration : PhoneApplicationPage 
    { 
     public static string DeviceIDAsString; 
     public registration() 
     { 

      InitializeComponent(); 

      //selction.Items.Add("india"); 
      //selction.Items.Add("pakistan"); 
      //selction.Items.Add("china"); 
      //selction.Items.Add("USA"); 
      String[] name = { "india", "china", "pakistan" }; 
      String[] uname = { "Delhi", "Bijing", "Karachi" }; 
      String[] university = { "AIIMS", "MDU", "PGI" }; 
      String[] yeardate = { "2011", "2012", "2013" }; 
string[] question = { "what is your pet name", "what is your childhood name", "what is your mother name" }; 
      this.country.ItemsSource = name; 
      this.city.ItemsSource = uname; 
      this.university.ItemsSource = university; 
      this.year.ItemsSource = yeardate; 
      this.question.ItemsSource = question; 
       } 

     private void Image_Tap_1(object sender, System.Windows.Input.GestureEventArgs e) 
     { 

      if (txtusername.Text == "") 
      { 
       MessageBox.Show("enter the name"); 
      } 
      else if (txtid.Text == "") 
      { 
       MessageBox.Show("enter valid id"); 
      } 
      else if (txtpassword.Password == "") 
      { 
       MessageBox.Show("enter the password"); 
      } 
      else if (txtconfirm.Password == "") 
      { 
       MessageBox.Show("enter the same password again "); 
      } 
      else if (txtemail.Text == "") 
      { 
       MessageBox.Show("enter the valid email id "); 
      } 
      else if (txtmobileno.Text == "") 
      { 
       MessageBox.Show("enter the valid 10 digit mobile no "); 
      } 


      if (txtpassword.Password != txtconfirm.Password) 
      { 
       MessageBox.Show("password doesnot match please enter same password"); 
      } 



      SendPost(); 
      //getDeviceId(); 


     } 
     //private static String getDeviceId() 
     //{ 
     // //byte[] id = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId"); 
     // //return BitConverter.ToString(id).Replace("-", string.Empty); 
     // // get the unique device id for the publisher per device 

     //} 

     void SendPost() 
     { 


      byte[] myDeviceID = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId"); 

      DeviceIDAsString = Convert.ToBase64String(myDeviceID); 

      Uri url = new Uri(" http://www.mobileoid2.co/docbase/index.php?methodname=createuser"); 

      // Create the web request object 
      HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
      webRequest.Method = "POST"; 

      webRequest.Headers["username"] = txtusername.Text; 
      webRequest.Headers["userid"] = txtid.Text; 
      webRequest.Headers["password"] = txtpassword.Password; 
      webRequest.Headers["confirmpaswword"] = txtconfirm.Password; 
      webRequest.Headers["email"] = txtemail.Text; 
      webRequest.Headers["mobileno"] = txtmobileno.Text; 
      webRequest.Headers["country"] = country.SelectedItem.ToString(); 
      webRequest.Headers["city"] = city.SelectedItem.ToString(); 
      webRequest.Headers["university"] = university.SelectedItem.ToString(); 
      webRequest.Headers["year"] = year.SelectedItem.ToString(); 
      webRequest.Headers["question"] = question.SelectedItem.ToString(); 
      webRequest.Headers["uniqueid"] = DeviceIDAsString; 
      webRequest.ContentType = "application/json;charset=utf-8"; 
      //"text/json";// 
      // Start the request 
      webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest); 
     } 
     void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
     { 


      HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
      // End the stream request operation 
      Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); 



      string postData = "username" + txtusername.Text + ".userid" + txtid.Text + "." + ".password" + txtpassword.Password + "." + ".confirmpassword" + txtconfirm.Password + "." + ".email" + txtemail.Text + "." + ".mobileno" + txtmobileno.Text + "." + "." + ".country" + country.SelectedItem.ToString() + "." + "." + ".city" + city.SelectedItem.ToString() + "." + "." + ".university" + university.SelectedItem.ToString() + "." + "." + ".year" + year.SelectedItem.ToString() + "." + ".question" + question.SelectedItem.ToString() + "." + ".uniqueid" + DeviceIDAsString +"."; 
      var input = JsonConvert.SerializeObject(postData); 
      byte[] byteArray = Encoding.UTF8.GetBytes(input); 

      // Add the post data to the web request 
      postStream.Write(byteArray, 0, byteArray.Length); 
      postStream.Close(); 

      // Start the web request 
      webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); 
     } 
     void GetResponseCallback(IAsyncResult asynchronousResult) 
     { 
      try 
      { 
       HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
       HttpWebResponse response; 

       // End the get response operation 
       response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); 
       Stream streamResponse = response.GetResponseStream(); 
       StreamReader streamReader = new StreamReader(streamResponse); 
       var Response = streamReader.ReadToEnd(); 
       //outputbox.Text = Response.ToString(); 
       streamResponse.Close(); 
       streamReader.Close(); 
       response.Close(); 

      } 
      catch (WebException e) 
      { 
       // Error treatment 
       // ... 
      } 
     } 



     private void Image_Tap_2(object sender, System.Windows.Input.GestureEventArgs e) 
     { 
      NavigationService.Navigate(new Uri("/docapp;component/login.xaml", UriKind.RelativeOrAbsolute)); 
     } 

     } 

    } 
+0

请格式化你的代码片段正确 – Ari

+0

出现“UnauthorizedAccessException说:”这一切,什么东西在OS(或在服务器端)不允许您发布该数据。第一步是找出哪个位拒绝你 - 在调试器中尝试查看,或者添加一个日志记录负载。 – gbjbaanb

+0

错误来自字符串发布数据。 – rozy

回答

0

不知道为什么,但是当访问通常抛出此错误不允许。也许您尝试使用的凭据是不允许的!

问题在于操作系统拒绝由于I/O错误导致的请求,其中需要证书,或者您没有在那里读取/写入的权限,或者服务器需要用户名/密码匹配允许你。类似的东西。尝试使用

try { 
    // code here to try.. 
} catch (System.UnauthorizedAccessException e) { 
    // error e.Message 
} 

这里捕获它是从MSDN博客帖子备注:

的UnauthorizedAccessException例外,通常由包装Windows API调用的方法抛出。要查找异常的原因,请检查异常对象的Message属性的文本。

了解更多:http://msdn.microsoft.com/en-us/library/system.unauthorizedaccessexception(v=vs.110).aspx

+0

code deos not goes below string post data?你能提出为什么如此吗? – rozy

+0

@rozy,是的,因为异常是在它下面的那一行被捕获的。字符串postdata处的行被执行,并且下一行被陷入错误。因此抛出异常... –

+0

此@afzaal的任何解决方案 – rozy

相关问题