来自Windows Phone 8的XML HTTP发布请求

本文关键字:布请求 请求 HTTP Phone Windows XML 来自 | 更新日期: 2023-09-27 18:28:14

我使用以下代码将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应用程序中运行此代码时,会出现异常System.UnauthorizedAccessException

我已经检查了WMAppManifest.xml中的所有权限。有人能建议我在windows phone 8上做错了什么吗。

来自Windows Phone 8的XML HTTP发布请求

代码的问题很简单,当你在后台线程中时,你试图访问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();
}       

希望它能有所帮助!