1

次のコードを使用して、HTTP 要求を PHP ベースの Web サービスに送信しています。

namespace xyz
{
class Test
{
    private ManualResetEvent allDone = new ManualResetEvent(false);


    // Main begins program execution.
    public void SendRequest()
    {

        MessageBox.Show( "inside send request" );

        // Create a new HttpWebRequest object.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.domainname.com/Test.php");

        request.ContentType = "application/x-www-form-urlencoded";

        // Set the Method property to 'POST' to post data to the URI.
        request.Method = "POST";

        // start the asynchronous operation
        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

        // Keep the main thread from continuing while the asynchronous 
        // operation completes. A real world application 
        // could do something useful such as updating its user interface. 

        allDone.WaitOne();
    }

    private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {

        MessageBox.Show("inside get request stream");

        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        Stream postStream = request.EndGetRequestStream(asynchronousResult);

        //Console.WriteLine("Please enter the input data to be posted:");
        string postData = "Message = Hello";

        // Convert the string into a byte array. 
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Write to the request stream.
        postStream.Write(byteArray, 0, postData.Length);
        postStream.Close();

        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private void GetResponseCallback(IAsyncResult asynchronousResult)
    {

        MessageBox.Show("inside get response");

        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        Console.WriteLine(responseString);
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse
        response.Close();
        allDone.Set();
    }       
}
}

このコードを C# コンソール アプリケーションで実行すると、問題なく動作します。しかし、Windows 8 の c C# Phone Application でこのコードを実行しようとすると、例外が発生しますSystem.UnauthorizedAccessException

のすべての権限も確認しましたWMAppManifest.xml。誰かが私がWindows Phone 8で間違っていることを提案できますか.

4

1 に答える 1

1

コードの問題は単純です。バックグラウンド スレッドにいる間に UI スレッドにアクセスしようとします。MessageBox.Show() メソッドを呼び出そうとすると、コードが失敗します。

Dispatcher.BeginInvoke を使用してメッセージを表示してみてください。

private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    Dispatcher.BeginInvoke(() => 
    {
        MessageBox.Show("inside get request stream");
    });

    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    Stream postStream = request.EndGetRequestStream(asynchronousResult);

    //Console.WriteLine("Please enter the input data to be posted:");
    string postData = "Message = Hello";

    // Convert the string into a byte array. 
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    // Write to the request stream.
    postStream.Write(byteArray, 0, postData.Length);
    postStream.Close();

    // Start the asynchronous operation to get the response
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}

private void GetResponseCallback(IAsyncResult asynchronousResult)
{

    Dispatcher.BeginInvoke(() => 
    {
        MessageBox.Show("inside get response");
    });

    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
    Stream streamResponse = response.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse);
    string responseString = streamRead.ReadToEnd();
    Console.WriteLine(responseString);
    // Close the stream object
    streamResponse.Close();
    streamRead.Close();

    // Release the HttpWebResponse
    response.Close();
    allDone.Set();
}       

それが役に立てば幸い !

于 2013-08-20T07:31:01.370 に答える