gpt4 book ai didi

c# - 在 Windows Phone 8 上的 Unity 中发出发布请求

转载 作者:太空狗 更新时间:2023-10-29 17:58:04 24 4
gpt4 key购买 nike

我正在尝试从 Unity 平台在 Windows Phone 8 上执行发布请求。我不想使用 unity WWW 方法,因为这会阻止渲染(并且不是线程安全的)。

以下代码在编辑器和 Android 上都有效,但是在为 WP8 构建它时出现以下错误。

System.Byte[] System.Net.WebClient::UploadData(System.String,System.String,System.Byte[])` doesn't exist in target framework.

这里解释了这个错误的原因

It’s because Windows Phone 8 uses a different flavor of .NET called .NET for Windows Phone which is missing some of the types available on other platforms. You’ll have to either replace these types with different ones or implement them yourself. - http://docs.unity3d.com/Manual/wp8-faq.html

这是我的代码

using (WebClient client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "application/json";

byte[] requestData = new byte[0];
string jsonRequest = "{}";
if (data != null)
{
string tempRequest = Converter.SerializeToString (data);
jsonRequest = "{\"Data\": \"" + tempRequest + "\"}";

requestData = System.Text.Encoding.UTF8.GetBytes(jsonRequest);
}

// below line of code is the culprit
byte[] returnedData = client.UploadData(url, "POST", requestData);

if(returnedData.Length > 0)
{
// do stuff
}
}

我也尝试过 WebRequests,但 GetResponse() 破坏了它,并且 HttpClient 不存在。

那么,在 Windows Phone 8 上,如何在不使用 WWW 的情况下在 Unity 中发布数据?

根据评论请求更新 - WebRequests

此代码使用 HttpWebRequest 在编辑器和 Android 上运行,但在 Windows Phone 上会抛出代码下方列出的错误。

var request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(url); 
request.ContentType = "application/json";
request.Method = "POST";

var sw = new System.IO.StreamWriter(request.GetRequestStream(), System.Text.Encoding.UTF8);

sw.Write(jsonRequest); // jsonRequest is same variable as in above code, string with json object.
sw.Close();

var re = request.GetResponse();

string resultString = "";
using (var outputStream = new System.IO.StreamReader(re.GetResponseStream(), System.Text.Encoding.UTF8))
{
resultString = outputStream.ReadToEnd();
}

if(resultString.Length > 0)
{}

错误一:

Error: method System.IO.Stream System.Net.HttpWebRequest::GetRequestStream() doesn't exist in target framework.

错误 2:

System.Net.WebResponse System.Net.HttpWebRequest::GetResponse() doesn't exist in target framework.

更新更多细节 - UploadStringAsync

使用此代码发出异步请求,它在编辑器中再次运行良好,但在 WP8 上会抛出错误。

bool isCompleted = false;
byte[] returnedData = null;
client.UploadDataCompleted +=
new UploadDataCompletedEventHandler((object sender, UploadDataCompletedEventArgs e) =>
{
Debug.Log("return event");
returnedData = e.Result;
isCompleted =true;
});

Debug.Log("async call start");
client.UploadDataAsync(new Uri(url), requestData);

while(isCompleted == false){
Thread.Sleep(100);
}

if(returnedData.Length > 0)
{}

错误 1

method System.Void System.Net.WebClient::add_UploadDataCompleted(System.Net.UploadDataCompletedEventHandler) doesn't exist in target framework.

错误 2

Error: method System.Void System.Net.WebClient::UploadDataAsync(System.Uri,System.Byte[]) doesn't exist in target framework.

错误 3

Error: type System.Net.UploadDataCompletedEventArgs doesn't exist in target framework.

错误 4

Error: method System.Byte[] System.Net.UploadDataCompletedEventArgs::get_Result() doesn't exist in target framework.

最佳答案

我不知道 Unity 是否对您施加了任何潜在限制,但 Windows Phone 8 有 WebClient.UploadStringAsync methodWebClient.UploadStringCompleted event因为这样做。

HttpWebRequest 也应该有效(同样,我不知道任何 Unity 限制 - 请参阅上面的评论要求澄清)。

关于c# - 在 Windows Phone 8 上的 Unity 中发出发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31037396/

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