![](/img/trans.png)
[英]Sending HTTP POST Request with XML to a web service and reading response data in C# for windows 8 phone
[英]XML HTTP Post Request from Windows Phone 8
我正在使用以下代碼將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#電話應用程序中運行此代碼時,它給出了System.UnauthorizedAccessException
異常。
我還在WMAppManifest.xml
檢查了所有權限。 有人可以建議Windows Phone 8在做什么嗎?
代碼上的問題很簡單,當您在后台線程中時嘗試在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();
}
希望能幫助到你 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.