初めてのスタックオーバーフロー。
わかりましたので、クラスプロジェクト用に作成しているこのアプリがあり、Windows Phone 8 の投稿要求を実装しようとしています。私は C# と Windows Phone 開発の初心者です。これは私の最初の試みですが、プログラミングの原則をよく理解しています。
問題は、postData でテキスト ボックスの Text プロパティを割り当てる直前または直後に、Visual Studio がデバッガ コンソールでいくつかの例外を返すことです。
なぜこれが起こるのか、そして/または何が起こっているのかを理解するために私が勉強すべきC#/ Windows Phone Devの資料を理解するのを手伝ってくれる人はいますか?
どんな助けでも大歓迎です-事前にThnx。
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);
}
}
}
}