我正在开发wp8应用程序。我的问题是,当我点击注册按钮并且发生以下异常并且代码不在字符串发布数据之下时,应用程序出错了
System.Windows.ni.dll 中出现System.UnauthorizedAccessException
类型的例外,但未在用户代码中处理
如果存在此异常的处理程序,则程序可以安全地继续
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 :(得分: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