Windows phone 8 HttpWebRequest发布WP8
第一次堆栈溢出 好的,我有一个为班级项目制作的应用程序,我正在尝试为WindowsPhone8实现一个post请求。我是C#和windows phone开发的新手。这是我的第一次尝试,但我对编程原理有很好的理解 问题是VisualStudio在我将textbox的Text属性指定给postData之前或之后,在调试器控制台中返回了一些异常 有谁能帮我弄清楚为什么会发生这种情况,或者我应该学习什么样的C#/Windows Phone开发材料来了解发生了什么 非常感谢您的帮助-请提前告知Windows phone 8 HttpWebRequest发布WP8,windows-phone-8,httpwebrequest,Windows Phone 8,Httpwebrequest,第一次堆栈溢出 好的,我有一个为班级项目制作的应用程序,我正在尝试为WindowsPhone8实现一个post请求。我是C#和windows phone开发的新手。这是我的第一次尝试,但我对编程原理有很好的理解 问题是VisualStudio在我将textbox的Text属性指定给postData之前或之后,在调试器控制台中返回了一些异常 有谁能帮我弄清楚为什么会发生这种情况,或者我应该学习什么样的C#/Windows Phone开发材料来了解发生了什么 非常感谢您的帮助-请提前告知 An ex
An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
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 but was not handled in user code
这是代码
using System;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp1.Resources;
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void ButtonFuction(object sender, RoutedEventArgs e)
{
var request = HttpWebRequest.Create("http://www.foo.com") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
// Create the post data
string postData = "blah=" + textBlock1.Text + "&blah=" + textBlock2.Text + "&blah=moreblah";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
//Start the web request
request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request);
}
void GetResponceStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
string result = httpWebStreamReader.ReadToEnd();
//For debug: show results
Debug.WriteLine(result);
}
}
}
}
首先,每当您遇到这种让您不知所措的情况(未处理的异常)时,请确保捕获并检查它
try
{
// code poops exception
}catch(Exception ex)
{
// place a breakpoint below
var thisIsAllYouNeed = ex.ToString();
}
您可以从中获得异常、所有内部异常和堆栈跟踪。把它和糟糕的代码一起粘贴到一个问题中,你几乎总能得到答案(如果你自己没有弄明白的话)
您的问题是您没有指定需要使用网络。你不能在不告诉手机所有者你要做什么的情况下就获取地理位置、网络、短信等资源。否则,我会把你的联系人列表发送到我在俄罗斯的服务器上,而你却在享受我可爱的cat应用程序
因此,你需要编辑你的应用程序,并在手机上指定你的应用程序工作所需的所有受控资源。你可以找到。阅读它,看看你的应用程序使用了什么
现在我们知道真正的问题是线程。异步操作在一个线程上开始,在另一个线程上结束(通常不做额外的工作)。因此,
buttonfaction
在一个线程(UI线程)上启动,GetRequestStreamCallback
在另一个线程上运行
UI元素具有“线程关联性”,这意味着它们只能被UI线程中执行的代码触及。我会让你寻找原因。您试图在非UI线程的线程上触摸UI元素(textBlock1.Text
)。因此,解决办法是停止这样做
哦,怎么了?两种不同的方式。您可以使用UI线程在UI线程上调用一个方法,但您真的希望在完成所有后台线程工作之前不要这样做。因此,为了使这尽可能简单,在UI线程上读取内容,将其放入一个临时变量,然后在后台线程上读取
private void ButtonFuction(object sender, RoutedEventArgs e)
{
// Create the post data
_lolThreading = "blah=" + textBlock1.Text + "&blah=" + textBlock2.Text + "&blah=moreblah";
var request = HttpWebRequest.Create("http://www.foo.com") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
private string _lolThreading;
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
// avoid bad-touching UI stuff
byte[] byteArray = Encoding.UTF8.GetBytes(_lolThreading);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
//Start the web request
request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request);
}
现在,您可能希望在完成后跳过UI线程(而不是将内容扔到
调试
),因此您必须使用UI调度程序
(例如textBlock1.Dispatcher
)调用调用(()=>textBlock1.Text=“某些结果等”)代码>(注意,这不是那么容易,你必须先研究如何调用)。首先,每当你遇到这种情况(未处理的异常)让你不知所措时,一定要抓住并检查它
try
{
// code poops exception
}catch(Exception ex)
{
// place a breakpoint below
var thisIsAllYouNeed = ex.ToString();
}
您可以从中获得异常、所有内部异常和堆栈跟踪。把它和糟糕的代码一起粘贴到一个问题中,你几乎总能得到答案(如果你自己没有弄明白的话)
您的问题是您没有指定需要使用网络。你不能在不告诉手机所有者你要做什么的情况下就获取地理位置、网络、短信等资源。否则,我会把你的联系人列表发送到我在俄罗斯的服务器上,而你却在享受我可爱的cat应用程序
因此,你需要编辑你的应用程序,并在手机上指定你的应用程序工作所需的所有受控资源。你可以找到。阅读它,看看你的应用程序使用了什么
现在我们知道真正的问题是线程。异步操作在一个线程上开始,在另一个线程上结束(通常不做额外的工作)。因此,buttonfaction
在一个线程(UI线程)上启动,GetRequestStreamCallback
在另一个线程上运行
UI元素具有“线程关联性”,这意味着它们只能被UI线程中执行的代码触及。我会让你寻找原因。您试图在非UI线程的线程上触摸UI元素(textBlock1.Text
)。因此,解决办法是停止这样做
哦,怎么了?两种不同的方式。您可以使用UI线程在UI线程上调用一个方法,但您真的希望在完成所有后台线程工作之前不要这样做。因此,为了使这尽可能简单,在UI线程上读取内容,将其放入一个临时变量,然后在后台线程上读取
private void ButtonFuction(object sender, RoutedEventArgs e)
{
// Create the post data
_lolThreading = "blah=" + textBlock1.Text + "&blah=" + textBlock2.Text + "&blah=moreblah";
var request = HttpWebRequest.Create("http://www.foo.com") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
private string _lolThreading;
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
// avoid bad-touching UI stuff
byte[] byteArray = Encoding.UTF8.GetBytes(_lolThreading);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
//Start the web request
request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request);
}
现在,您可能希望在完成后跳过UI线程(而不是将内容扔到调试
),因此您必须使用UI调度程序
(例如textBlock1.Dispatcher
)调用调用(()=>textBlock1.Text=“某些结果等”)代码>(注意,这不是那么容易,你必须先研究如何调用)。我也遇到了同样的错误,不知道如何找出错误所在,多亏了这里的答案,我发现我没有使用我的应用程序所需的所有功能
现在,我学会了如何在发生这种情况时进行调查:)
谢谢 我也面临着同样的错误,不知道如何找出错误所在,多亏了这里的答案,我发现我没有使用我的应用程序所需的所有功能
现在,我学会了如何在发生这种情况时进行调查:)
谢谢 功能已启用。结果表单重试捕获:System.UnauthorizedAccessException:无效的跨线程访问。位于System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)处System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp)处System.Windows.Controls.TextBox.get_Text()处PhoneApp1.MainPage.GetRequestStreamCallback(IAsyncResult asynchro