简体   繁体   English

Windows Phone 8的XML HTTP发布请求

[英]XML HTTP Post Request from Windows Phone 8

I am using the following code to send the HTTP request to PHP based web service. 我正在使用以下代码将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();
    }       
}
}

When I run this code in a C# console application, it works fine. 当我在C#控制台应用程序中运行此代码时,它工作正常。 But when I try tu run this code in c C# Phone Application for windows 8 it gives an exception System.UnauthorizedAccessException . 但是,当我尝试在Windows 8的C C#电话应用程序中运行此代码时,它给出了System.UnauthorizedAccessException异常。

I have checked all the permissions also in WMAppManifest.xml . 我还在WMAppManifest.xml检查了所有权限。 Can someone suggest what am I doing wrong for windows phone 8. 有人可以建议Windows Phone 8在做什么吗?

The problem on your code is simple, you try to access on the UI Thread while you are in a background Thread. 代码上的问题很简单,当您在后台线程中时尝试在UI线程上进行访问。 Your code fail when you try call the MessageBox.Show() method. 当您尝试调用MessageBox.Show()方法时,代码将失败。

Try to use the Dispatcher.BeginInvoke to display your message : 尝试使用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();
}       

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM