2013-08-20 44 views
1

我正在使用以下代碼將HTTP請求發送到基於PHP的Web服務。來自Windows Phone的XML HTTP發佈請求8

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#電話應用程序中它給出了一個例外System.UnauthorizedAccessException

我已經在WMAppManifest.xml中檢查了所有權限。有人可以建議我爲Windows Phone 8做了什麼錯誤。

+0

你的網址是不是本地主機?您是否添加了訪問網絡的功能? –

+0

不是它的本地主機....是的,我檢查了'WMAppManifest.xml'中的'ID_CAP_NETWORKING'。 – Mayank

+0

我得到'運行代碼時發生mscorlib.ni.dll異常時發生'System.IO.FileNotFoundException'類型的第一次機會異常 – Mayank

回答

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(); 
}  

希望它能幫助!

+0

我已經從代碼中刪除了'MessageBox.Show()'。並且我在函數GetResponseCallback()中放置了一個斷點。當我運行代碼時,這個斷點永遠不會到達。 – Mayank