gpt4 book ai didi

c# - 来自 Windows Phone 8 的 XML HTTP Post 请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:06 25 4
gpt4 key购买 nike

我正在使用以下代码将 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# Phone 应用程序中运行此代码时,它给出了一个异常 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();
}

希望对您有所帮助!

关于c# - 来自 Windows Phone 8 的 XML HTTP Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com